Magnus9998 Posted April 21, 2011 Share Posted April 21, 2011 It may look like something silly, but I have to ask for help because I'm in despair. What I have to do is to update a single row in a SQL table. The table is called 'posts', and the row I'm trying to update is called 'Replies'. I only need to add 1 to the value of the 'Replies' row. This is the code I tried. $result = mysql_query("SELECT Replies FROM Posts WHERE Post_ID = $postid2", $link) or die(mysql_error()); $row = @mysql_fetch_array($result); //This is to avoid "undefined variable" warning. do{ echo('<br>Row:' . $row['Replies'] . '<br>'); $reps = (int)$row['Replies']; echo('<br>Numero: ' . $reps . '<br>'); $rtot = $reps++; echo('<br>Total: ' . $rtot . '<br>'); $save = mysql_query("UPDATE posts SET Replies = '$rtot' WHERE Post_ID = '$postid2'", $link) or die(mysql_error()); } while ($row = @mysql_fetch_array($result)); However, it doesn't work. It gets the value of the row, or that's what I think, but it doesn't add 1 to the value of the row. It returns this: Row:0 Numero: 0 Total: 0 'Numero' means 'Number', it's the value of the row, and 'Total' is supposed to be Row + 1. How can I solve this? Please, help me! Quote Link to comment https://forums.phpfreaks.com/topic/234340-i-need-help-with-updating-a-single-field-in-my-table/ Share on other sites More sharing options...
joe92 Posted April 21, 2011 Share Posted April 21, 2011 You can just do: mysql_query("UPDATE posts SET Replies = Replies + 1 WHERE Post_ID = '$postid2'", $link) What is $link? Quote Link to comment https://forums.phpfreaks.com/topic/234340-i-need-help-with-updating-a-single-field-in-my-table/#findComment-1204446 Share on other sites More sharing options...
mikosiko Posted April 21, 2011 Share Posted April 21, 2011 @Joe92 http://php.net/manual/en/function.mysql-query.php Quote Link to comment https://forums.phpfreaks.com/topic/234340-i-need-help-with-updating-a-single-field-in-my-table/#findComment-1204451 Share on other sites More sharing options...
Magnus9998 Posted April 21, 2011 Author Share Posted April 21, 2011 You can just do: mysql_query("UPDATE posts SET Replies = Replies + 1 WHERE Post_ID = '$postid2'", $link) What is $link? Thank you very much for your reply! It worked with only your line. I knew it has to be something that simple, but I was sick of searching in google with no good results. You saved my day. Oh, and $link is a variable that contains the credentials to connect to the database. $link = mysql_connect("host", "user", "password"); It's needed in order to make the SQL queries. Quote Link to comment https://forums.phpfreaks.com/topic/234340-i-need-help-with-updating-a-single-field-in-my-table/#findComment-1204557 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.