douhong1703 2014-08-27 17:56
浏览 59
已采纳

SQL语法错误:无法正确提交表单

I get this error once I submit my form. As I have not used PDO too much yet, my code may have obvious mistakes. It is intended to update the table with form data and read it. How can I fix it?

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'UPDATE edit SET content=':bodyBackgroundColour' WHERE id='bodyBackgroundColour';' at line 2

try
{
    $database = new PDO ('mysql:host=localhost;dbname=youdonotneedtoknow;charset=utf8', 'youdonotneedtoknow', 'youdonotneedtoknow');
    $database -> setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $database -> setAttribute (PDO::ATTR_EMULATE_PREPARES, false);

    //Update
    if (!empty ($_POST))
    {
    $statement = $database -> prepare ("
    UPDATE edit SET content=':windowTitle' WHERE id='windowTitle';
    UPDATE edit SET content=':bodyBackgroundColour' WHERE id='bodyBackgroundColour';
    UPDATE edit SET content=':divisionBackgroundColour' WHERE id='divisionBackgroundColour';
    ");
    $statement -> execute (array (
    ':windowTitle' => $_POST ['windowTitle'],
    ':bodyBackgroundColour' => $_POST ['bodyBackgroundColour'],
    ':divisionBackgroundColour' => $_POST ['divisionBackgroundColour'],
    ));
    }
}
catch (PDOException $exception)
{
    die ('Error: ' . $exception -> getMessage () . '<br><br>' . 'Por favor diríjase a "index.php" en este mismo directorio y contacte a su administrador.');
}
  • 写回答

1条回答 默认 最新

  • dongmei1828 2014-08-27 18:07
    关注

    There are 2 errors in the codes.

    #1

    Placeholders are not required to be surrounded by quotes. Therefore the statement should be:

    UPDATE edit SET content=:windowTitle WHERE id='windowTitle';
    UPDATE edit SET content=:bodyBackgroundColour WHERE id='bodyBackgroundColour';
    UPDATE edit SET content=:divisionBackgroundColour WHERE id='divisionBackgroundColour';
    

    #2

    executing statement requires PDO::ATTR_EMULATE_PREPARES to set to 1 or true. Also, it requires PHP 5.3+ and mysqlnd driver.


    Advice: Use transactions. Execute each statement one by one, and at last commit / rollback depends on your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部