Home  >  Q&A  >  body text

Using PHP PDO prepared statements for update operations

<p>I'm trying to update my database using the following query: </p> <pre class="brush:php;toolbar:false;">$sth = "UPDATE rpacks SET rpacks_location VALUES (:location) WHERE rpacks_id = (:id)"; $q = $conn->prepare($sth); $q->execute(array(':location'=>$location, ':id'=>$id));</pre> <p><b>But I get this error</b></p> <p><code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 There is an error in your SQL syntax; check the Manual for the correct syntax to use near 'VALUES ('test') WHERE rpacks_id = ('2')' on line 1 of </code></p>
P粉668019339P粉668019339394 days ago369

reply all(2)I'll reply

  • P粉315680565

    P粉3156805652023-08-25 17:13:59

    change to:

    $sth = "Update rpacks SET rpacks_location = :location WHERE rpacks_id = :id";

    reply
    0
  • P粉248602298

    P粉2486022982023-08-25 13:33:14

    There is an error in your update query because you used the insert query syntax.

    The following is the correct query:

    $sql = "UPDATE rpacks SET rpacks_location = :location WHERE rpacks_id = :id";
    $stmt = $conn->prepare($sql);
    $stmt->execute([':location'=>$location, ':id'=>$id]);

    refer to: http://dev.mysql.com/doc/refman/5.0/ en/update.html

    reply
    0
  • Cancelreply