0

I'm retrieving last id of tbl_orderdetail(table 1) in order to add addon items entries into tbl_orderdetail_addon(table 2). Basically it should insert into (table 2) as per the number of ids received from (table 1). I'm using mysql_insert_id() for that.

i'm getting that right but the ids are not looping through. It inserts the same id of (table 1) for all entries in (table 2). Say that I have two entries in (table 1),obviously it should return 2 id's.And each id has 2 add on items .So in (table 2) there should be four entries with 2 different id's of (table 1). IN my case Im getting 4 entries of same id of (table 1).
Here's my coding:

$o_id_detail=mysql_insert_id();//(table 1) id
foreach($addon_price as $a_p=>$p)// 
     { 
         echo $a_p;
           foreach($p as $m)
              {

                 $addon_id= $m['id'];
                 echo $m['addon_name'];
                 echo $m['deposit'];
                 echo $m['ppd'];
                 echo $m['pp_eight'];
                 echo $m['pp_six'];


$addon_total=$m['deposit']+$m['ppd']+$m['pp_eight']+$m['pp_six'];
                 echo $addon_total;



                  $addon_detail="INSERT INTO tbl_orderdetail_addon (OrderID,addOns_id,addOns_price) VALUES ('$o_id_detail','$addon_id','$addon_total')";
                               if(!empty($addon_id))
                                {
                                  mysql_query($addon_detail)or die(mysql_error());


                                 }
                    }     

        }

Any help would be greatly appreciated.Thanks in advance.

clarkson
  • 33
  • 4

1 Answers1

1

Basically it should insert into (table 2) as per the number of ids received from (table 1)

mysql_insert_id() only returns a single int value. As described in the manual:

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

You need to insert the first row to table1 then the corresponding rows in table2; insert the second row to table1 then the corresponding rows in table2; etc.

Pseudo-code to explain the previous paragraph:

foreach order_detail {
    insert order_detail;
    get last_insert_id;
    foreach order_detail_addon {
        insert order_detail_addon with last_insert_id;
    }
}

Edited to add:

Your code is vulnerable to SQL Injection. Read this and also see what the PHP manual says about it.

And you should stop using the mysql_* functions.

Community
  • 1
  • 1
timclutton
  • 12,682
  • 3
  • 33
  • 43
  • sorry I dont get it...what you mean by "You need to insert the first row to table1 then the corresponding rows in table2; insert the second row to table1 then the corresponding rows in table2; etc."...How to do that please? – clarkson Aug 07 '14 at 02:56
  • I tried your suggestion and yeah you get what I mean but the problem lies in displaying the addon items details indeed.Thats why I used the above foreach loop. But I'm just lost! – clarkson Aug 08 '14 at 09:40
  • What do you mean by '...the problem lies in displaying the addon items details...'? Your question is about inserting items to the database, not retrieving them. – timclutton Aug 09 '14 at 21:14