2012-09-26 28 views
1

嘿傢伙我有一點問題。我只想在我的表格中插入一個外鍵值。 這是我在mysql中創建表語句。在mysql中插入一個外鍵值(codeigniter)

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 | fkmember | date_of_sales |

sales_line表 line_id | fkproduct | fksales | quantity_purchased |小計|

我在兩個表中插入值碼:

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的值? 因爲我想在一輪中插入這兩個表。請幫我一把。謝謝

回答

2

試試這個(請注意使用的$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); 
    } 
+0

感謝傢伙,這對我來說真的很有幫助。我非常感謝你的幫助。 – rochellecanale

+0

這是正確答案嗎?如果是這樣,請確保您單擊答案旁邊的複選標記。 :d – seangates

0

在你的控制器功能,插入銷售進入第一,並返回銷售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(); 
} 

如果正確,請不要忘記接受任何答案。