Skip to content
PHP Logo

How can email addresses be leveraged to ensure that users can only participate in a survey once in PHP?

To ensure that users can only participate in a survey once using their email addresses, you can store the email addresses of participants in a database table and check if the email address already exists before allowing them to take the survey.

php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=survey_db', 'username', 'password');

// Check if the email address already exists in the database
$email = $_POST['email'];
$stmt = $pdo->prepare("SELECT * FROM participants WHERE email = :email");
$stmt->bindParam(':email', $email);
$stmt->execute();

if($stmt->rowCount() > 0) {
    echo "You have already participated in the survey.";
} else {
    // Allow the user to take the survey
    // Insert the email address into the database
    $insertStmt = $pdo->prepare("INSERT INTO participants (email) VALUES (:email)");
    $insertStmt->bindParam(':email', $email);
    $insertStmt->execute();

    echo "Thank you for participating in the survey!";
}