What are the best practices for handling form submissions in PHP to ensure data integrity and prevent duplicate entries?

To ensure data integrity and prevent duplicate entries when handling form submissions in PHP, it is recommended to validate the input data, sanitize it to prevent SQL injection attacks, and check for duplicate entries before inserting the data into the database.

// Validate and sanitize form data
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

// Check for duplicate entries
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->bindParam(':email', $email);
$stmt->execute();
if ($stmt->rowCount() > 0) {
    // Duplicate entry found, handle accordingly
} else {
    // Insert data into the database
    $stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->execute();
}