0

I'm having an issue where the form that inserts into the database is supposed to check if there's already a duplicate entry in the table and tell the user to go back if it is a duplicate, but even when I purposely duplicate the data, it still adds to the form.

I borrowed the code from here, making my own changes: Check for duplicates before inserting

Below is my code:

$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();
Barmar
  • 741,623
  • 53
  • 500
  • 612
Xero1
  • 329
  • 4
  • 18
  • Are you using `mysql` or `mysqli`? `$conn->query()` is for `mysqli`, but `mysql_query()` is for `mysql`. – Barmar Oct 05 '17 at 00:16
  • most probably because $date_requested isn't the same for both requests. Set a unique index on email and it should at least not lead to dupes. – LordNeo Oct 05 '17 at 00:22
  • have you looked to see the date structure that your front end is supplying? I would start there to see if the data that you think you are querying on is in the correct structure. – Margaret Oct 05 '17 at 05:00
  • I've converted mysql_query() to mysqli_query() and any mysql tags. Still duplicates. I've also needed to state that the Date Request is from a drop down, so the guest can't Type 2 versions of the same date. It is generated on the front end by the Jquery UI calendar. – Xero1 Oct 05 '17 at 16:58
  • I just ran another test where I only checked to see if emails are in the DB and removed the other condition, and it still posts. So it's not an issue of the values... – Xero1 Oct 05 '17 at 17:04
  • Rewrote it and got it fixed using this:https://stackoverflow.com/questions/18008191/mysqli-insert-but-only-if-not-a-duplicate – Xero1 Oct 05 '17 at 17:52

0 Answers0