What are common pitfalls when using PHP and MySQL together in a form submission scenario?

One common pitfall is not properly sanitizing user input before inserting it into the database, leaving the application vulnerable to SQL injection attacks. To solve this, always use prepared statements or parameterized queries to sanitize user input before executing SQL queries.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Sanitize user input
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);

// Prepare SQL statement
$stmt = $mysqli->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);

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

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