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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?