We're dealing with a string here and your variable in the query needs to be quoted.
Sidenote: With or without the apostrophe, your query would still have errored out.
- Consult a prepared statement example that I have prepared further below.
Also consult the following which may be of help:
So change
SET name=" . $temp)
to read as
SET name= '$temp'")
A prepared statement would be better to use here though. Consult the following
References:
and escape your data using the escaping function relative to the MySQL API used to connect with.
However, you don't have a WHERE
clause and that will update your entire table.
So, and for example:
SET name= '$temp' WHERE column_to_update = 'x' ")
the x
is what you need to fill in and modify the column name to suit.
Reference on UPDATE:
Example from the manual (PDO/INSERT):
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
So in your case:
try {
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "
UPDATE chasiv_lg_cbbfl.defaults
SET `name` = :temp
WHERE `column_to_update` = :xxx
";
$query = $conn->prepare($sql);
$query->bindValue(":temp", $temp);
$query->bindValue(":xxx", $xxx);
$query = $statement->execute();
$conn = null; // Disconnect
}
catch(PDOException $e) {
echo $e->getMessage();
}