Answer the question
In order to leave comments, you need to log in
What should be the SQL query syntax in php?
Good afternoon. In phpMyAdmin I make a SQL query with the following content:
UPDATE `users` SET `name`='Vlad' WHERE 1
Next, I want to insert this query into a php file so that the user can make changes to his profile, naturally transforming it.
I have:
$query = mysql_query("UPDATE `users` SET `name`=".$name." WHERE ".$id."");
$name - New username
$id - user number in the database
Where is the incorrectness in the request?
Answer the question
In order to leave comments, you need to log in
The SQL syntax is no different.
The only difference is the way of variable substitution.
Firstly, queries should be executed not through mysql_query, but through PDO
. Secondly, there should be question marks in place of variables , and the variables themselves should be passed to the request during execution:
$stmt = $pdo->prepare("UPDATE `users` SET `name`=? WHERE id=?");
$stmt->execute(array($name, $id));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question