0

I am working on a small project and is having some issues with looping MySQL insert. I currently have 2 database tables. I am fetching information from one to another.

table with data:

$q = "SELECT * FROM HARDWARE WHERE ID_2=".$db->qstr(20);
$rss = $db->execute($q);
$re2=$rss->GetArray();

so I am getting the array of data fine.

inserting the data only if the id is {20} Currently I have 2 rows with ID_2 = 20 but it is only inserting one row not both. here is my insert query.

$sql = "INSERT INTO PARTS SET
          IN_ID     =". $db->qstr($in_id).",
      ER_ID     =". $db->qstr( $er_id).",
      ITEM      =". $db->qstr( $re2[0]['ITEM']   ).",
      NAME      =". $db->qstr( $re2[0]['NAME']   );

it inserts the data fine, just one row not multiple rows. any suggestions?

Thanks.

user3620142
  • 135
  • 2
  • 3
  • 13

1 Answers1

0

Insert your query in a loop

foreach ($re2 as $r): $sql = "INSERT INTO PARTS SET IN_ID =". $db->qstr($in_id).", ER_ID =". $db->qstr( $er_id).", ITEM =". $db->qstr( $r['ITEM'] ).", NAME =". $db->qstr( $r['NAME'] ); endforeach;

or use multiple inserts Inserting multiple rows in mysql

both should be fine at what you have there.

Community
  • 1
  • 1
Marius.C
  • 700
  • 6
  • 14