What are some common pitfalls to avoid when working with user authentication and data manipulation in PHP scripts?

One common pitfall to avoid when working with user authentication in PHP scripts is storing passwords in plain text. To enhance security, passwords should be hashed before storing them in the database. Another pitfall is not validating user input properly, which can lead to SQL injection attacks. It is crucial to sanitize and validate user input to prevent such vulnerabilities.

// Storing passwords securely using password_hash() function
$password = "password123";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Validating and sanitizing user input to prevent SQL injection
$username = $_POST['username'];
$password = $_POST['password'];

$username = mysqli_real_escape_string($conn, $username);
$password = mysqli_real_escape_string($conn, $password);

// Perform SQL query with sanitized input
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);