What are some best practices for updating SQL data using PHP variables?

When updating SQL data using PHP variables, it is important to sanitize the input to prevent SQL injection attacks. One common method is to use prepared statements with placeholders for the variables to be updated. This ensures that the variables are securely passed to the SQL query without risking injection.

// Assume $conn is the database connection object

// Sanitize input variables
$variable1 = mysqli_real_escape_string($conn, $variable1);
$variable2 = mysqli_real_escape_string($conn, $variable2);

// Prepare the SQL statement with placeholders
$sql = "UPDATE table_name SET column1 = ?, column2 = ? WHERE condition";

// Prepare the statement
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $variable1, $variable2);

// Execute the statement
$stmt->execute();

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