What are some common pitfalls to avoid when writing PHP scripts to modify user data in a database?

One common pitfall to avoid when writing PHP scripts to modify user data in a database is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries to safely interact with the database.

// Example code snippet using prepared statements to modify user data in a database
// Assume $conn is the database connection object

// Get user input
$user_id = $_POST['user_id'];
$new_email = $_POST['new_email'];

// Prepare SQL statement
$stmt = $conn->prepare("UPDATE users SET email = ? WHERE id = ?");
$stmt->bind_param("si", $new_email, $user_id);

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

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