Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert a foreign key values in mysql (codeigniter)
    primarykey
    data
    text
    <p>hey guys i have a little problem. All i want is to insert a foreign key values in my table. Here’s my create table statement in mysql.</p> <pre><code>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`) ) </code></pre> <p>my table structure:</p> <p>sales table sales_id | fkmember | date_of_sales |</p> <p>sales_line table line_id | fkproduct | fksales | quantity_purchased | subtotal |</p> <p>my code in inserting the values in two tables:</p> <pre><code>foreach($products as $p){ $data = array( 'sales_id' =&gt; null, 'fkmember' =&gt; $memberid 'name' =&gt; $p['product_name'] ); $this-&gt;db-&gt;insert('sales',$data); } foreach($products as $p){ $data = array( 'line_id' =&gt; null, 'fk_product' =&gt; $p['id'], 'fk_sales' =&gt; null, 'quantity_purchased' =&gt; $p['product_qty'], 'subtotal' =&gt; number_format($subtotal,2) ); $this-&gt;db-&gt;insert('sales_line',$data); } } </code></pre> <p>i know i have an error in inserting values in inserting values in fk_sales. How can i insert a value in this field that comes from the id of my sales table? Because i want to insert these two tables in one round. Please Help Me Guys. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload