Jump to content

MYSQL Query INSERT in WHILE loop


thecase

Recommended Posts

Hi,

 

Here is an extract of my while loop

 

while ($personquery = mysql_fetch_array($personfetch, MYSQL_ASSOC))
          {
           
          echo "$personfetch[first_name]";
} 

 

This will list each person now I want to do an INSERT to the database on every single member that is being listed I can only get it to INSERT for the one.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/
Share on other sites

For each person that is echoed I want them to be inserted into members tables. I tried doing

 

mysql_query ("INSERT INTO `current_members` (`id`, `membername`) 
VALUES
('', '$personfetch[first_name]') ");

 

Although this simply inserts just the first name and not all of the list. Hope this is clearer

EDIT: Maq beat me to it, but I'll post this anyway

=====================================

 

As Maq insinuated you don't need to pull records using a SQL statement just to insert them in another table. You can do everything with a single query. Just use the first query (which you don't show) to generate the values for the select statement. Also, since I assume id is an auto-increment row you do not need to include it in the INSERT query.

 

$query = "INSERT INTO `current_members` (`membername`)
          SELECT first_name FROM sometable";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.