嘿傢伙我有一點問題。我只想在我的表格中插入一個外鍵值。 這是我在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的值? 因爲我想在一輪中插入這兩個表。請幫我一把。謝謝
感謝傢伙,這對我來說真的很有幫助。我非常感謝你的幫助。 – rochellecanale
這是正確答案嗎?如果是這樣,請確保您單擊答案旁邊的複選標記。 :d – seangates