-1

I'm trying to do a UPDATE query in php and I get a syntax error

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id = '30'' at line 1

 $sql = "UPDATE usuario SET nombre WHERE id = '$id_usuario'";
try {
    $stmt = $conn->prepare($sql);
    $result = $stmt->execute();
    if ($result) {
        echo "Ok";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
    die;
}

the error is in the $sql = ... line. Thanks!

Chariot
  • 335
  • 1
  • 3
  • 13
  • Your error does not belong with this query. The query you show is searching for `email`, while your error says it's searching for `id`. Unless $email contains `something' WHERE id = '30`, in which case you really ought to be using prepared statements and parameter binding. – aynber Jan 21 '20 at 19:22
  • or do you have a trigger in your db? – Jens Jan 21 '20 at 19:23
  • @aynber yes, sorry xD – Chariot Jan 21 '20 at 19:25

1 Answers1

1

UPDATE usuario SET nombre WHERE id = '$id_usuario'

You didn't provide a new value to update. Your query should look like:

UPDATE usuario SET nombre = :new_name WHERE id = :id_usuario
The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • Ok...just a neglect... – Chariot Jan 21 '20 at 19:30
  • 2
    This is wide open to [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Greg Schmidt Jan 21 '20 at 19:31
  • this is not the way using prepared statements same in question @Chariot See here for proper prepared statements in pdo or MySQL I : https://phpdelusions.net/pdo#prepared –  Jan 21 '20 at 19:32