What are the consequences of not validating form input data in PHP before submitting it to the database?
If form input data is not validated before submitting it to the database in PHP, it can lead to security vulnerabilities such as SQL injection attacks or data corruption. To solve this issue, always validate and sanitize user input before inserting it into the database.
// Validate and sanitize form input data before submitting to the database
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$age = filter_var($_POST['age'], FILTER_VALIDATE_INT);
// Insert validated data into the database
$query = "INSERT INTO users (name, email, age) VALUES ('$name', '$email', $age)";
// Execute the query