Jump to content

I need help with updating a single field in my table


Magnus9998

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.