calmchess Posted January 9, 2010 Share Posted January 9, 2010 I need to select some auto incremented ids out of a database and then use those id in a where statement which updates the database at the id's location the below code works up to the update statements where it just dies without coughing up an error or anything......can you spot whats wrong and help me fix it? $current = Array(); $currsel = mysql_query("select id from page0.page0 where refnum ='$newnum' AND username!='admin'") or die(mysql_error()); while($currrow = mysql_fetch_array($currsel)){ array_push($current,$currrow['id']); } print_r($current); $current1 = Array(); $currsel1 = mysql_query("select id from page0.page0 where refnum ='$oldnum' AND username!='admin'") or die(mysql_error()); while($currrow1 = mysql_fetch_array($currsel1)){ array_push($current1,$currrow1['id']); } print_r($current1); $upref1= mysql_query("update page0.page0 SET refnum='$newnum' where id='$current[0]' OR id='$current[1]'OR id='$current[2]'OR'id=$current[3]'") or die(mysql_error()); $upref11= mysql_query("update page0.page0 SET refnum='$oldnum' where id='$current1[0]'OR id='$current1[1]'OR id='$current1[2]'OR id='$current1[3]'") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/ Share on other sites More sharing options...
kayess2004 Posted January 9, 2010 Share Posted January 9, 2010 Hi, In your update statement, change you OR's to AND's HTH Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991674 Share on other sites More sharing options...
calmchess Posted January 9, 2010 Author Share Posted January 9, 2010 Thanks for the reply.......I've tried using AND ....still doesn't work.......its driving me mad because looking at the code everything should work just fine.....the id go into the array when i print it out just fine......it just the update won't update at each integer that is in the array.. Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991733 Share on other sites More sharing options...
spfoonnewb Posted January 9, 2010 Share Posted January 9, 2010 It's hard to gather exactly what you want to accomplish from the code itself. Are you trying to select an ID from the database WHERE the username is NOT equal to admin, and then perform an update on it? Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991740 Share on other sites More sharing options...
calmchess Posted January 9, 2010 Author Share Posted January 9, 2010 select id where $refnum = 1 and is not admin stick the id number in an array then perform update on only those numbers in the array. Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991746 Share on other sites More sharing options...
spfoonnewb Posted January 9, 2010 Share Posted January 9, 2010 Try this example: <?php /* WHERE ID = This Number */ $oldnum = "1"; $current = array(); $currsel = mysql_query("SELECT `id` FROM `page0` WHERE `refnum` = '$oldnum' AND `username` != 'admin'") or die(mysql_error()); if (mysql_num_rows($currsel) > 0) { while($row = mysql_fetch_array($currsel)) { $current[] = $row['id']; } $updateStr = array(); foreach ($current as $var) { $updateStr[] = "`id` = '{$var}'"; } echo "<pre>"; print_r($updateStr); echo "</pre>"; $updateStr = (count($updateStr) > 1) ? implode(" OR ", $updateStr) : $updateStr[0]; echo "UPDATE `page0` SET --what you want to update-- WHERE ($updateStr) AND `username` != 'admin'"; } else { echo "No Records Found."; } ?> Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991761 Share on other sites More sharing options...
calmchess Posted January 9, 2010 Author Share Posted January 9, 2010 ok i've integrated this my only question is where does the update statment go? i only see half an update statement being echoed.....do i just change that into a mysql statment? Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991770 Share on other sites More sharing options...
spfoonnewb Posted January 9, 2010 Share Posted January 9, 2010 I'm not exactly sure what you wanted to update, so you need to modify this so that its correct: UPDATE `page0` SET --what you want to update-- WHERE ($updateStr) AND `username` != 'admin' Then simply change the echo to a mysql_query if its right. Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991771 Share on other sites More sharing options...
calmchess Posted January 9, 2010 Author Share Posted January 9, 2010 this didn't do quite what i wanted but thanks for tryin i'll keep trying to get my code to work it does everything i need it to do it just won't use the variables in the update statment if i chang the variables to integers then it works.....it a variable problem for some reason unknown to me.....take care and I like your php skills Link to comment https://forums.phpfreaks.com/topic/187823-my-sql-select-id-and-then-update-problem/#findComment-991779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.