Jump to content

Recommended Posts

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!

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.