Jump to content

trying $foo++ from SQL result


pezkingrich2

Recommended Posts

I\'m trying to add one to the \'comment count\' when someone submits a comment! The sql error i get is:

You have an error in your SQL syntax near \'INSET INTO users (comments) VALUES (\'7\') WHERE username=\'Rich\'\' at line 1

 

Code:

$get_comment = mysql_query(\"SELECT comments FROM users WHERE username=\'$username\'\") or die(mysql_error());

$new_comment = $get_comment++;

 

mysql_query(\"INSET INTO users (comments) VALUES (\'\".$new_comment.\"\') WHERE username=\'$username\'\") or die(mysql_error());

echo \"Comment entered. Thanks!<BR>n\";

 

 

 

The comment value is stored in the databse as an INTEGER. I tried echoing the values, for $new_comment i get \'7\' and for $get_comment i get \'Resource id #6\' . Ahh wht am i doing wrong?! (i manually entered the value 5 for comments).

Link to comment
Share on other sites

I know that problem is solved but I just noticed the line

 

[php:1:970cc41601]<?php

$new_comment = $get_comment++;

?>[/php:1:970cc41601]

 

As you are using \'post-increment\', thisis saying:

 

Assign value in $get_comment to $new_comment then increment $get_comment

 

What you wanted was a pre-increment :

[php:1:970cc41601]<?php

$new_comment = ++$get_comment;

?>[/php:1:970cc41601]

 

which increments $get_comment then assigns the new value to $new_comment

 

hth

Link to comment
Share on other sites

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.