douhong1703 2014-08-28 01: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-28 02: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.

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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊