tags:

views:

68

answers:

4

It seems there is a parse error with my mysql and php code can some please help me clean up this code.

     $tag = mysql_real_escape_string($_POST['tag']);
     $query = 'UPDATE tags SET count = count+1 WHERE tag = '.$tag;
     mysql_query($query,$dbc);
     if( !mysql_affected_rows() ) {
       $query = 'INSERT INTO tags (tag,count) VALUES('.$tag.',1)';
       if (mysql_query($query,$dbc));
       {
       die('Error: ' . mysql_error());
       }
     echo "1 record added";

     mysql_close($dbc)

A: 
   $tag = mysql_real_escape_string($_POST['tag']);
    $query = 'UPDATE tags SET count = count+1 WHERE tag = "'.$tag.'"';
    mysql_query($query,$dbc);
    if( !mysql_affected_rows() ) {
      $query = 'INSERT INTO tags (tag,count) VALUES("'.$tag.'",1)';
      if (!mysql_query($query,$dbc))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    }

    mysql_close($dbc);

Missing a semicolon on the last line

PS: Giving us the parse error makes finding the answer much easier.

Chacha102
I get this error now Parse error: syntax error, unexpected $end in
Remove the semicolon at the end of the 2nd if statement. (I fixed my code)
Chacha102
And You forgot to close the first if statement
Chacha102
it kind of works now, but it wont update my database now wierd.
The if statement with the mysql_query() needs to be checking for a false, right now it checks for a true, which means that the query worked.
Chacha102
how can i change it?
I just fixed it in my code.
Chacha102
No it still wont work:(
Well, if it isn't updating you actual mysql database, you might have a mysql error or something, which I can't really fix.
Chacha102
SHould I post the whole script?
Well, first try and debug it. Put echos in all of the if statements to see exactly what is happening. (What is the query, are there any mysqsl_error()s, etc)
Chacha102
No more mysql errors the form is displayed I did something wrong though?
Yes, something is wrong if it isn't updating. You might want to open up another question/answer about that, since it is a separate problem. In that you might want to post the full code too.
Chacha102
okay thanks for all the help. and thanks every one for trying.
Use 'single quotes', not "double-quotes" for SQL string literal delimiters. Double-quotes is MySQL-only that means something quite different in ANSI SQL and will fail on most other databases, as well as on MySQL itself under various configurations. Single quotes work everywhere.
bobince
A: 
$tag = mysql_real_escape_string($_POST['tag']);
$query = 'UPDATE tags SET count = count + 1 WHERE tag = "'.$tag.'"';
mysql_query($query,$dbc);
if( !mysql_affected_rows() ) {
  $query = 'INSERT INTO tags (tag,count) VALUES("'.$tag.'",1)';
  if (!mysql_query($query,$dbc));
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($dbc);
jusunlee
who gave me a negative point? i added quotations so that mysql will accept the queries... completely unacceptable.
jusunlee
The reason Negative points are anyomous is just that. I gave you a negative because right now the first if statement won't worth, and the second if statement won't work.
Chacha102
wow youre answer won't work because you don't have quotations for the string tag. Good job mate promoting your answer that doesn't work.
jusunlee
Look again.
Chacha102
way to edit your answer to "reflect" what i did. thanks a bunch chacha.
jusunlee
wow this system is ridiculous... you just gave me a negative point and then proceeded to copy my answer to your post. great job little man.
jusunlee
I'm just trying to help the guy. That is the goal of the forums. It isn't a competition, it is trying to be helpful. Combining ideas from multiple answers is helpful, and creates the most correct answer.
Chacha102
dude you voted my answer down WHICH was CORRECT unlike YOURS. way to HELP. right.
jusunlee
IT ISN'T CORRECT. BOTH OF YOU IF STATEMENTS WON'T WORK. Fix it, and I'll remove the downvote. Very simple.
Chacha102
This is complete abuse of the system. You just voted my answer down and proceeded to copy mine to correct your original answer.
jusunlee
You know what, go complain to Meta. See how many people don't care. meta.stackoverflow.com
Chacha102
btw your insert still wont work.. you need the double quotes to indicate string.
jusunlee
You almost found them all :-) Just missing the } for the first **if** statement...
jeroen
+1  A: 

Some problems: Missing }, ; after if, missing ;, dying on success (second query), $tag not in quotes:

$tag = mysql_real_escape_string($_POST['tag']);
$query = "UPDATE tags SET count = count+1 WHERE tag = '".$tag."'";
mysql_query($query,$dbc);
if( !mysql_affected_rows() ) {
   $query = "INSERT INTO tags (tag,count) VALUES('".$tag."',1)";
   if ( !mysql_query($query,$dbc) )
   {
       die('Error: ' . mysql_error());
   }
   echo "1 record added";
}

mysql_close($dbc);

That seems about it, at least if $dbc is a valid connection...

jeroen
You mean quotes and not parenthesis.
Gumbo
You're right...
jeroen
A: 

Actually the problem with your script like single quotes not ended properly.

Here is corrected code

<?php

$tag = mysql_real_escape_string($_POST['tag']);

    $query = "UPDATE tags SET count = count+1 WHERE tag = '".$tag."'";

    mysql_query($query,$dbc);

    if( !mysql_affected_rows() ) {

      $query = "INSERT INTO tags (tag,count) VALUES('".$tag.",1)";

      if (mysql_query($query,$dbc))

      {

      die('Error: ' . mysql_error());

      }

    echo "1 record added";


    mysql_close($dbc)

?>

santosh