What are common syntax errors to look out for when using PHP for SQL updates?

One common syntax error to look out for when using PHP for SQL updates is forgetting to properly concatenate variables within the SQL query string. This can lead to errors or SQL injection vulnerabilities. To solve this issue, make sure to concatenate variables using the dot (.) operator within the query string.

// Correct way to update a record in a database using PHP and SQL
$id = 1;
$newValue = "Updated Value";

// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Prepare and execute the SQL update query
$sql = "UPDATE table_name SET column_name = '" . $newValue . "' WHERE id = " . $id;
if ($connection->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $connection->error;
}

// Close the connection
$connection->close();