Mysql and PHP Problem?

似乎有一个解析错误,我的MySQL和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)


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


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


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)

?>
链接地址: http://www.djcxy.com/p/93650.html

上一篇: 从VARCHAR转换为INT

下一篇: Mysql和PHP的问题?