Can't find my Query syntax error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brainintraining
    New Member
    • May 2007
    • 1

    Can't find my Query syntax error

    Maybe my eyes are just tired...
    Can anyone help me spot the syntax error in this code:
    [PHP]$sql = "INSERT INTO `images` ( 'imgid' , 'lstid' , 'descrip' )
    VALUES (
    '', '" ."$pid". "', ''
    );";


    $result = @mysql_query("$ sql") or die(mysql_error ());[/PHP]

    I get the error:

    You have an error in your SQL syntax near ''imgid' , 'lstid' , 'descrip' ) VALUES ( '', '15', '' )' at line 1

    thanks in advance for your help!!!
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Use backticks (`) for column names and single quotes (') for values:

    [code=php]
    $sql = "INSERT INTO `images` (`descrip`) VALUES ('$pid')";

    if(! ($result = mysql_query($sq l)))
    throw new Exception(mysql _error());
    [/code]

    Couple of minor things:
    • If a value will be null, you don't have to include it in the list of fields.
    • You're not supposed to end your queries with a semi-colon when you pass them to mysql_query. Don't believe me? http://php.net/mysql_query. Look under "query".

    Comment

    Working...