TeddyKiller Posted April 18, 2010 Share Posted April 18, 2010 My pc hugely crashed out, as my while function ended up adding about rows into the database until it got to page 17477 (Which was when I managed to close the browser) What I want to do.. for each user registered with activated = 1, to insert a row into another table with there user_id. What happens is.. it does what it's suppose to do.. nearly, what it doesn't do.. is 1 - It keeps inserting rows in, even with the same user_id. So it's for some reason not checking if it exists or not. 2 - Doesn't stop. Now, whats the problem? $query = mysql_query("select * from users where activated = '1'"); while($row = mysql_fetch_array($query)) { $query1 = mysql_query("select * from " . $tablename . ""); if(mysql_num_rows($query1) > 0) { while($rw = mysql_fetch_array($query1)) { if($row['id'] == $rw['user_id']) {} else { $query2 = mysql_query("insert into " . $tablename . " (user_id) VALUES ('".$row['id']."')"); } } } else { $query3 = mysql_query("insert into " . $tablename . " (user_id) VALUES ('".$row['id']."')"); } } Cheese. EDIT: That was suppose to be 'cheers' Link to comment https://forums.phpfreaks.com/topic/198916-this-while-function-never-stops-inserting-into-database/ Share on other sites More sharing options...
trq Posted April 18, 2010 Share Posted April 18, 2010 It keeps inserting rows in, even with the same user_id. So it's for some reason not checking if it exists or not. You have no logic in place to check if the record exists or not, what do you expect? The code looks seriously dodgy to me. SELECT queries should never need to be executed in loops. Link to comment https://forums.phpfreaks.com/topic/198916-this-while-function-never-stops-inserting-into-database/#findComment-1044129 Share on other sites More sharing options...
TeddyKiller Posted April 18, 2010 Author Share Posted April 18, 2010 if($row['id'] == $rw['user_id']) {} else { Thats my logic of checking if it exists or not.. What should I do instead? I'm complete clueless. Link to comment https://forums.phpfreaks.com/topic/198916-this-while-function-never-stops-inserting-into-database/#findComment-1044132 Share on other sites More sharing options...
trq Posted April 18, 2010 Share Posted April 18, 2010 What I want to do.. for each user registered with activated = 1, to insert a row into another table with there user_id. if ($result = mysql_query("SELECT id FROM users WHERE activated = 1")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { if ($result1 = mysql_query("SELECT id FROM $tablename WHERE id = {$row['id']}")) { if (!mysql_num_rows($result1)) { mysql_query("INSERT INTO $tablename (user_id) VALUES ({$row['id']})"); } } } } } Its still a horrible method, where is $tablename coming from? Link to comment https://forums.phpfreaks.com/topic/198916-this-while-function-never-stops-inserting-into-database/#findComment-1044136 Share on other sites More sharing options...
TeddyKiller Posted April 18, 2010 Author Share Posted April 18, 2010 $tablename is $_POST['tablename'], only in the admin panel though. It's probably not the most securest method. Was only going to be a temporary fix... to run it once, then take it off. Link to comment https://forums.phpfreaks.com/topic/198916-this-while-function-never-stops-inserting-into-database/#findComment-1044138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.