简体   繁体   中英

looping mysql query to get multiple rows of data from a table and insert into another table

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM