What common syntax errors should be avoided when using MySQL UPDATE commands in PHP?

One common syntax error to avoid when using MySQL UPDATE commands in PHP is forgetting to include the WHERE clause. This can result in updating all rows in the table instead of just the intended rows. Another error is using single quotes instead of backticks for table or column names, which can cause syntax errors. It's important to properly escape and sanitize user input to prevent SQL injection attacks.

// Example of a correct MySQL UPDATE command in PHP with proper syntax and input sanitization
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Sanitize user input
$user_id = $mysqli->real_escape_string($_POST['user_id']);
$new_email = $mysqli->real_escape_string($_POST['new_email']);

// Update user email in the users table
$sql = "UPDATE users SET email='$new_email' WHERE id=$user_id";
if ($mysqli->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $mysqli->error;
}

$mysqli->close();