Jump to content


Photo

trying $foo++ from SQL result


  • Please log in to reply
6 replies to this topic

#1 pezkingrich2

pezkingrich2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 August 2003 - 08:34 PM

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).

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 22 August 2003 - 08:44 PM

Thats was more of spelling mistake not \"INSET\" its \"INSERT\"

Try this

mysql_query("INSERT INTO users (comments) VALUES (\'$new_comment\') WHERE username=\'$username\'") or die(mysql_error()); echo "Comment entered. Thanks!<BR>n";

Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 pezkingrich2

pezkingrich2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 August 2003 - 08:48 PM

ahh, i knew that.. thanks, but still not solved, now the error is:

You have an error in your SQL syntax near \'WHERE username=Rich\' at line 1[/b]

#4 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 22 August 2003 - 08:53 PM

How can a insert have a where clause???

Try to use a update if you wnat to update!!!!! :roll:
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#5 pezkingrich2

pezkingrich2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 August 2003 - 08:59 PM

ok, i feel kinda stupid now, thanks! apart from...... the post count doesn\'t achually update. It seems to stick on 7.

#6 pezkingrich2

pezkingrich2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 August 2003 - 09:04 PM

problem solved!

UPDATE users SET comments=comments+1 WHERE username=\'$username\'

I couldn\'t get it to add 1 in php, but this works in mysql. Thanks for your help mod.

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 26 August 2003 - 08:17 AM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users