What are common pitfalls when trying to save form data into a SQL database using PHP?

One common pitfall when saving form data into a SQL database using PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to securely insert data into the database.

// Assuming $conn is the database connection object

// Sanitize user input
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$message = mysqli_real_escape_string($conn, $_POST['message']);

// Prepare and bind SQL statement
$stmt = $conn->prepare("INSERT INTO messages (name, email, message) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $message);

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

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