我遇到一个问题,即插入数据库的表单应该检查表中是否已经有重复的条目,并告诉用户如果它是重复的则返回,但即使我故意复制了数据,它仍然添加到表格中。
我从这里借用了代码,进行了自己的更改:Check for duplicates before inserting
以下是我的代码:
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$encore_number = $_POST['encore_number'];
$guests = $_POST['guests'];
$date_requested = $_POST['date_requested'];
$comments = $_POST['comments'];
$checkbox = $_POST['subscribe'];
$dupesql = "SELECT * FROM Learn_to_play WHERE (email = '$email' AND date_requested = '$date_requested' AND guests = '$guests')";
$duperaw = mysql_query($dupesql);
if(mysql_num_rows($duperaw) > 0) {
echo 'We see that the email ' . $email . 'has already registered for ' . $date_requested . ' with ' . $guests . '. Please go back and select a different date.';
} else {
$sql = "INSERT INTO Learn_to_play (firstname, lastname, email, encore_number, guests, date_requested, comments, subscribe) VALUES ('$first_name','$last_name','$email','$encore_number','$guests','$date_requested','$comments','$checkbox')";
echo 'inserted into db';
}
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
// echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();