What steps should be taken to troubleshoot issues with setting flags in a SQL database using PHP?

Issue: When setting flags in a SQL database using PHP, ensure that the SQL query is correctly constructed and executed. Check for any syntax errors or incorrect data types in the query that may be causing the issue. Additionally, verify that the database connection is established properly before executing the query.

// Sample code snippet to set a flag in a SQL database using 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);
}

// Construct and execute SQL query to set a flag
$flag = 1;
$sql = "UPDATE table_name SET flag = $flag WHERE id = 1";

if ($conn->query($sql) === TRUE) {
    echo "Flag set successfully";
} else {
    echo "Error setting flag: " . $conn->error;
}

// Close database connection
$conn->close();