What are common pitfalls when using if statements in PHP to update database records based on form submissions?

One common pitfall when using if statements in PHP to update database records based on form submissions is not properly sanitizing user input, which can lead to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries to securely interact with the database.

// Assuming a form submission with a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id = $_POST['id'];
    $new_value = $_POST['new_value'];

    // Connect to the database
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Prepare a SQL statement using a prepared statement
    $stmt = $conn->prepare("UPDATE table_name SET column_name = ? WHERE id = ?");
    $stmt->bind_param("si", $new_value, $id);
    $stmt->execute();

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