What is the best practice for setting a flag in a SQL database using PHP?

To set a flag in a SQL database using PHP, you can execute an SQL UPDATE query that sets the flag column to a specific value based on a condition. This can be achieved by using a parameterized query to prevent SQL injection attacks. Make sure to establish a database connection before executing the query and handle any errors that may occur during the process.

<?php
// Establish a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// Set a flag in the database
$id = 1; // Example ID
$flag = 1; // Example flag value

$stmt = $conn->prepare("UPDATE table_name SET flag = ? WHERE id = ?");
$stmt->bind_param("ii", $flag, $id);

if ($stmt->execute()) {
    echo "Flag set successfully";
} else {
    echo "Error setting flag: " . $conn->error;
}

// Close connection
$stmt->close();
$conn->close();
?>