What common mistakes do beginners make when writing PHP scripts?

One common mistake beginners make when writing PHP scripts is not properly escaping user input, leaving their code vulnerable to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries when interacting with a database to prevent malicious input from being executed as SQL commands.

// Incorrect way without proper escaping
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);

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