What is the purpose of filtering input data in PHP forms before database entry?

Filtering input data in PHP forms before database entry is important to prevent SQL injection attacks and ensure data integrity. By sanitizing and validating user input, you can protect your database from malicious queries and errors caused by incorrect data. This practice also helps improve the overall security of your application and maintain the quality of the data stored in your database.

// Example of filtering input data in PHP form before database entry

// Assuming $conn is the database connection object

// Sanitize and validate user input
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

// Prepare SQL statement with sanitized data
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";

// Execute SQL statement
mysqli_query($conn, $sql);