How can PHP be used to prevent duplicate entries in a database when a form is submitted multiple times?
To prevent duplicate entries in a database when a form is submitted multiple times, you can check if the entry already exists in the database before inserting a new record. This can be done by querying the database with the form data to see if a matching entry already exists. If a matching entry is found, you can display an error message to the user instead of inserting a duplicate record.
// Assuming $conn is the database connection object and $formData is an array containing form data
// Check if the entry already exists in the database
$query = "SELECT * FROM your_table WHERE column_name = :value";
$stmt = $conn->prepare($query);
$stmt->bindParam(':value', $formData['value']);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Entry already exists, display an error message
echo "Entry already exists in the database.";
} else {
// Insert the new record into the database
$insertQuery = "INSERT INTO your_table (column_name) VALUES (:value)";
$insertStmt = $conn->prepare($insertQuery);
$insertStmt->bindParam(':value', $formData['value']);
$insertStmt->execute();
}