thecase Posted February 7, 2011 Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/ Share on other sites More sharing options...
Maq Posted February 7, 2011 Share Posted February 7, 2011 Sorry but what's the issue here? I don't even see an INSERT statement. If you're taking data from the database and inserting it back in then I don't think PHP is really needed unless you want to manipulate the data. Quote Link to comment https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/#findComment-1171108 Share on other sites More sharing options...
thecase Posted February 7, 2011 Author Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/#findComment-1171110 Share on other sites More sharing options...
Maq Posted February 7, 2011 Share Posted February 7, 2011 Instead of querying the database, then going through a loop, just INSERT the data directly in SQL. Check out: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html Quote Link to comment https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/#findComment-1171116 Share on other sites More sharing options...
Psycho Posted February 7, 2011 Share Posted February 7, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/226988-mysql-query-insert-in-while-loop/#findComment-1171117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.