What are some common mistakes people make when writing PHP scripts for MySQL database interactions?

One common mistake is not properly sanitizing user input before using it in SQL queries, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries when interacting with the database.

// Incorrect way without using prepared statements
$user_input = $_POST['username'];
$query = "SELECT * FROM users WHERE username = '$user_input'";
$result = mysqli_query($connection, $query);

// Correct way using prepared statements
$user_input = $_POST['username'];
$stmt = $connection->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $user_input);
$stmt->execute();
$result = $stmt->get_result();