Russia Posted March 19, 2011 Share Posted March 19, 2011 I have a script to update certain rows that contain certain data in them. Here is the code: if ($_POST['newstype'] == "1") { $query = mysql_query("SELECT * from `news` WHERE type='1'"); if (mysql_num_rows($query) == 1) { mysql_query("UPDATE `news` SET type = '2' WHERE type = '1' ORDER BY `news`.`dtime` ASC LIMIT 1") or die(mysql_error()); } else { echo "did not edit main news"; } } elseif ($_POST['newstype'] == "2") { $query = mysql_query("SELECT * from `news` WHERE type='2'"); if (mysql_num_rows($query) == 3) { mysql_query("UPDATE `news` SET type = '3' WHERE type= '2' ORDER BY `news`.`dtime` ASC LIMIT 1") or die(mysql_error()); } else { echo "did not edit recent news"; } } elseif ($_POST['newstype'] == "3") { $query = mysql_query("SELECT * from `news` WHERE type='3'"); if (mysql_num_rows($query) == 3) { mysql_query("UPDATE `news` SET type = '4' WHERE type= '3' ORDER BY `news`.`dtime` ASC LIMIT 1") or die(mysql_error()); } else { echo "did not edit old news"; } } else { echo "didnt update anything!"; } Here is my database structure. Why is it not updating it and always saying Did not edit main/recent/old news. Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/ Share on other sites More sharing options...
floridaflatlander Posted March 19, 2011 Share Posted March 19, 2011 Can you have or why do you want an ORDER BY, an ASC and a LIMIT in an UPDATE ? I use these things in SELECT queries to display info, but I'm fairly new to this stuff myself. What are you doing, retrieving info, updating info then storing the result? You're selecting a 2 or 4 or what ever and there is more than one to edit. I think if I did it, I do something like this with the update $guery = ("UPDATE `news` SET type = 'old or new value', title = 'old or new value', text1 = 'old or new value', etc. etc. WHERE newsid = '2' ") ; Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189711 Share on other sites More sharing options...
creata.physics Posted March 19, 2011 Share Posted March 19, 2011 I see a few issues with your script, first off, after every query is pulled, that needs the proper information, you use if (mysql_num_rows($query) == 1) { for the first type, and if (mysql_num_rows($query) == 3) { for the other two types, so the task under the argument will NOT PERFORM unless there is exactly 1 row, you need to check and see if there is at least 1 row to perform the update, I would change that to: if (mysql_num_rows($query) > 0) that means, if we found a row, that has $type == 1, we'll be able to grab all rows, but see, after your check to see if it has the right amount of rows, you only update 1 item, instead of all necessary items with the corresponding type. so after you check if the code has passed, i'd do this. while (mysql_fetch_assoc($query)) { mysql_query("UPDATE CODE HERE") } by looping and updating all fields that have that specific type, i think you can get where you need to go, you just need to alter your code a tad bit. Also, do not include ORDER BY or LIMIT within your update query unless you know you only want to update one field. Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189735 Share on other sites More sharing options...
Russia Posted March 20, 2011 Author Share Posted March 20, 2011 Also, do not include ORDER BY or LIMIT within your update query unless you know you only want to update one field. Finally someone that understands that I want to update only 1 field. But I have tried many things but its still not working. And yes sir I know that there is 1 row ATLEAST with the value its looking for. So why isnt it working? Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189751 Share on other sites More sharing options...
floridaflatlander Posted March 20, 2011 Share Posted March 20, 2011 $guery = ("UPDATE `news` SET type = 'old or new value', title = 'old or new value', text1 = 'old or new value', etc. etc. WHERE newsid = '2' ") ; Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189760 Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 I see, so you do NOT need while loops since you'll be updating ONE field. So what I need to do now to further assist you, is check and make sure the initial arguments are correct. if ($_POST['newstype'] == "1") { etc. So right above that code, can you add this ?><pre><?php print_r($_POST); ?></pre><?php and tell me what kind of output you get? Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189762 Share on other sites More sharing options...
floridaflatlander Posted March 20, 2011 Share Posted March 20, 2011 What are you doing, retrieving info, updating info then storing the result? Can you tell us (me anyway others maybe able to figure it out) what you are trying to do from beginning to end, in words? Look at the numbers in the type column and you'll see what I mean. The way I would do it if I wanted to edit one of the #4's is Run a SELECT query to get news of the same type these links go in a table on a page say news-tems.php I look in the table and see which news items I want to edit I click a link in the table that takes me to an edit page for that item, say edit.php I edit that item, one row, all rows or no rows if I change my mind, click submit or back and return to the table news-items.php . Quote Link to comment https://forums.phpfreaks.com/topic/231126-rows-not-being-updated-with-if-statement/#findComment-1189901 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.