What are the security implications of not properly handling special characters, such as apostrophes, in PHP scripts when inserting data into a database?

When special characters like apostrophes are not properly handled in PHP scripts when inserting data into a database, it can lead to SQL injection attacks. This vulnerability allows attackers to manipulate the SQL query and potentially access or modify sensitive data in the database. To prevent this, you should always sanitize user input by escaping special characters before inserting them into the database.

// Assuming $conn is the database connection object
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);

$query = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
mysqli_query($conn, $query);