What are the potential security risks associated with directly inserting form data into a database using PHP?

Directly inserting form data into a database using PHP without proper validation and sanitization can leave your application vulnerable to SQL injection attacks. To mitigate this risk, you should always sanitize and validate user input before inserting it into the database.

// Sanitize and validate form data before inserting into the database
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

// Prepare SQL statement with placeholders
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);

// Execute the statement
$stmt->execute();