What best practices should be followed when updating data in MySQL tables using PHP?

When updating data in MySQL tables using PHP, it is important to follow best practices to ensure data integrity and security. This includes using prepared statements to prevent SQL injection attacks, validating user input, and sanitizing data before executing the query.

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

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

// Prepare the update statement
$stmt = $mysqli->prepare("UPDATE table_name SET column1 = ?, column2 = ? WHERE id = ?");

// Bind parameters
$stmt->bind_param("ssi", $value1, $value2, $id);

// Set parameters and execute the statement
$value1 = "new_value1";
$value2 = "new_value2";
$id = 1;
$stmt->execute();

// Close the statement and connection
$stmt->close();
$mysqli->close();