You have an error in your SQL syntax; check the manual that corresponds to your MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dreamy
    New Member
    • Jul 2009
    • 29

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sum( ) VALUES ()' at line 1

    This is my code
    Code:
    <?php
    //make conection
    $name=$_POST['name'];
    $cloth=$_POST['cloth'];
    $bag=$_POST['bag'];
    
    $connection = mysql_connect("localhost", "root","") or die("Could not connect to MYSQL");
    $selection=mysql_select_db("addname") or die ("Unable to select database");
    $sql="SELECT * from sum WHERE name like '$name'"; 
    $result= mysql_query($sql) or die(mysql_error()); 
    $record = mysql_num_rows($result);  
    
       if ($record == 0)
       {
         $sql = "INSERT INTO sum( name, cloth, bag)  VALUES ('$name', '$cloth', '$bag')";
          $result= mysql_query($sql);
      
         if ($result)
         { echo "Record added: $name  $cloth  $bag "; }
           else   { die(mysql_error()); }     
       }
       else
         echo "<br>record already exist"; 
    ?>

    can some 1 help me about this problem,
    i used to fix it by changing the name, but now. it not success any more.
    thz.
    Last edited by Markus; Jul 27 '09, 08:59 AM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    In MySQL sum/SUM is a reserved word, that is, you cannot use it in a situation where it might be mistake for SQL syntax. The way to counter this is to surround any column-names table-names, etc., in backticks, like so:

    Code:
    INSERT INTO `tablename` (`field1`, `field2`, `field3`) VALUES (...)
    Also, Dreamy, please take a moment to read the Posting Guidelines - especially the part about 'how to post a question'. Use [code] tags when posting code.

    Furthermore, questions go in the 'answers' sub-forum - not in the 'insights' sub-forum.

    Mark.

    Comment

    Working...