PHP/ MySQL: Query not processing (syntax error near x on line 1)

Hey... so I attempted to run the query below and it just won't do it. I've checked:

  • The number of fields in the form matches the number of fields in the table.
  • The number of session variables matches those numbers too.
  • That the actual query contains the data to be input with an echo.
  • That the table name is correct.
  • That queries work with other tables in the same database (they DO).
  • Used or die(mysql_error(); to get the "Incorrect syntax" error.
  • Rebuilt the table several times.
  • Here's the line that's apparently syntactically problematic, if you can see anything wrong with it or point me in the right direction I'd be super greatful as I'm at a loss now! Might it be a memory issue?

    $sql = "INSERT INTO careBoiler (buyerID, date, sector, require, boilerMake, boilerModel, boilerType, heating, visit, deadline, budget, currcompany, specificreq) VALUES ('{$_SESSION['buyerID']}', now(), '{$_SESSION['cb']['sector']}', '{$_SESSION['cb']['require']}', '{$_SESSION['cb']['boilerMake']}', '{$_SESSION['cb']['boilerModel']}', '{$_SESSION['cb']['boilerType']}', '{$_SESSION['cb']['heating']}', '{$_SESSION['cb']['visit']}', '{$_SESSION['cb']['deadline']}', '{$_SESSION['cb']['budget']}', '{$_SESSION['cb']['currcompany']}', '{$_SESSION['cb']['specificreq']}')";
    

    Looks like require is a MySQL reserved word which you are using as a column name.

    To fix this you can either choose a different column name or place it in back ticks as:

    $sql = "INSERT INTO careBoiler (buyerID, date, sector, `require`,...
                                                           ^       ^
    

    It is a little difficult because you haven't provided the exact error message.

    However, it could be that you use the column name 'require' which is a reserved keyword in MySQL. The keyword 'date' is also reserved but due to it's widespread use it's allowed.

    Try changing the column name 'require' in your table and query.

    Look here for more information; http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

    If that's not it then please provide the full error message.


    As a general php/mysql debugging strategy

  • Print out the actual query, with the values inserted. Sometimes that alone will make the problem obvious.

  • If you have it, copy + paste the query into phpMyAdmin / mysql workbench and see what error message it gives you. If you don't have access to either of those, just call mysql_error() in the php script and see what error it gives you.

  • 链接地址: http://www.djcxy.com/p/69762.html

    上一篇: MySQL Php语法错误,查看手册

    下一篇: PHP / MySQL:查询未处理(第1行x附近的语法错误)