doufei8691 2014-05-19 23:59 采纳率: 100%
浏览 37
已采纳

使用GAE云sql进行PHP mySQL更新

Persons table has a field called "data" and another field called "id" that is automatically incremented. It has 1 entry and I am trying to update that entry. I am using GAE Cloud Sql for php. I successfully connect to the database then do the following:

$data="Please do not go.";

try {
                   $stmt = $db->prepare('INSERT INTO Persons (data) VALUES (:theData)');
                   $stmt->execute(array(':theData' => htmlspecialchars($data)));
                   $affected_rows = $stmt->rowCount();
                   // Log $affected_rows.
} catch (PDOException $ex) {
                    // Log error.
                    die(json_encode(
                    array('outcome' => false, 'message' => 'PIE.')
                    )
                    );
}

$data="Please go away.";

try {
          $stmt = $db->prepare('UPDATE Persons (data) VALUES (:theData) WHERE id=1');
          $stmt->execute(array(':theData' => htmlspecialchars($data)));
} catch (PDOException $ex) {
          // Log error.
          die(json_encode(
                    array('outcome' => false, 'message' => 'LOL YOU FAILED')
                    )
                    );
}

foreach($db->query('SELECT * from Persons') as $row) {

                    echo $row['data'];

}

$db = null;

The output is:

Please do not go.

Instead of:

Please go away.

What is wrong with the code I use to UPDATE the table?

  • 写回答

1条回答 默认 最新

  • doutu4335 2014-05-20 00:59
    关注

    UPDATE as documented here isn't formed like an INSERT using the VALUES keyword.

    UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]
    

    You instead want to specify the value/column relationships using SET

    UPDATE Persons SET data = :theData WHERE id=1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端