【问题标题】:Insert a foreign key values in mysql (codeigniter)在mysql中插入外键值(codeigniter)
【发布时间】:2012-09-17 14:34:47
【问题描述】:

大家好,我有一个小问题。我想要的只是在我的表中插入一个外键值。 这是我在 mysql 中的创建表语句。

CREATE TABLE `sales` (
 `sales_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
 `fkmember` INT(10) UNSIGNED NOT NULL,
 `date_of_sales` DATETIME NOT NULL,
 PRIMARY KEY (`sales_id`),
 INDEX `fkmember` (`fkmember`),
 CONSTRAINT `sales_ibfk_1` FOREIGN KEY (`fkmember`) REFERENCES `member` (`member_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;


CREATE TABLE `sales_line` (
 `line_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
 `fkproduct` INT(10) UNSIGNED NOT NULL,
 `fksales` INT(10) UNSIGNED NOT NULL,
 `quantity_purchased` INT(10) UNSIGNED NOT NULL,
 `subtotal` FLOAT(7,2) UNSIGNED NOT NULL,
 PRIMARY KEY (`line_id`),
 INDEX `fkproduct` (`fkproduct`),
 INDEX `fksales` (`fksales`),
 CONSTRAINT `sales_line_ibfk_1` FOREIGN KEY (`fkproduct`) REFERENCES `product` (`product_id`),
 CONSTRAINT `sales_line_ibfk_2` FOREIGN KEY (`fksales`) REFERENCES `sales` (`sales_id`)
) 

我的表结构:

销售表 sales_id |会员 | date_of_sales |

sales_line 表 line_id | fk产品 |销售 |购买数量 |小计 |

我在两个表中插入值的代码:

foreach($products as $p){
        $data = array(
            'sales_id' => null,
            'fkmember' => $memberid
            'name' => $p['product_name']
        );
        $this->db->insert('sales',$data);
    }
   foreach($products as $p){
        $data = array(
            'line_id' => null,
            'fk_product' => $p['id'],
            'fk_sales' => null,
            'quantity_purchased' => $p['product_qty'],
            'subtotal' => number_format($subtotal,2)
        );
        $this->db->insert('sales_line',$data);
    }
    } 

我知道在 fk_sales 中插入值时我在插入值时出错。 如何在此字段中插入来自我的销售表 id 的值? 因为我想在一轮中插入这两个表。请帮帮我,伙计们。谢谢

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    试试这个(注意使用$this->db->insert_id()):

        foreach($products as $p){
            $data = array(
                'sales_id' => null,
                'fkmember' => $memberid
                'name' => $p['product_name']
            );
            $this->db->insert('sales',$data);
        }
    
        $sales_insert_id = $this->db->insert_id();
    
        foreach($products as $p){
            $data = array(
                'line_id' => null,
                'fk_product' => $p['id'],
                'fk_sales' => $sales_insert_id,
                'quantity_purchased' => $p['product_qty'],
                'subtotal' => number_format($subtotal,2)
            );
            $this->db->insert('sales_line',$data);
        }
    

    【讨论】:

    • 谢谢大家,这对我帮助很大。非常感谢您的帮助。
    • 这是正确的答案吗?如果是这样,请确保单击答案旁边的复选标记。 :D
    【解决方案2】:

    看看Codeigniter Database Helper guide here。第一个函数是 $this->db->insert_id();

    所以你可以像这样使用它;

     $this->db->insert('sales',$data);
     $fk_sales_id = $this->db->insert_id();
    

    【讨论】:

      【解决方案3】:

      在您的控制器函数中,首先插入销售条目,并返回销售 id,然后使用返回的 id 插入销售行,如下所示。

      Eg: 
      //Controller function
      
        //insert the sales record and get the sales id
        $sales_id = $this->model_sale->insert_sale($mameber_id, $product_name); 
      
      foreach($products as $p)
      {
        //insert sales line
        $this->model_sales_line->insert_sales_line($sales_id, $p['id'],$p['product_qty'],$sub_total);
      }
      
      //Sales Model
      
      public function insert_sale($mameber_id, $product_name)
      {
       $data = array(            
              'fkmember' => $memberid
              'name' => $p['product_name']
          );
          $this->db->insert('sales',$data);
      
         return $this->db->insert_id();
      }
      

      如果正确,请不要忘记接受任何答案。

      【讨论】:

        猜你喜欢
        • 2020-02-28
        • 1970-01-01
        • 2011-01-02
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        相关资源
        最近更新 更多