0

I have tried looking all over the internet and using the answers provided in some of the questions, but cannot get this to work. How do I prevent PHP from inserting a duplicate record into the database? I can seem to get the echo statement of "no" to show up when a duplicate is being inserted, but cannot stop the actual duplicate record being inserted into the database.

I have tried almost everything, but nothing is working. Thank you to anyone who can help.

calender.php

<?php

 if(isset($_GET['add'])){
     $equipment=$_POST['equipment'];
     $start=$_POST['start'];
     $end=$_POST['end'];
     $notes=$_POST['notes'];
     $eventdate=$month."/".$day."/".$year;

     $sql="SELECT * FROM bookings WHERE equipment='$equipment' AND start_time='$start' AND selected_date='$eventdate'";
     $result= mysqli_query($dbconnection, $sql);
     $results=mysqli_num_rows($result);
     if($results>0) {
         echo "no";

     }

     $sqlinsert="insert into bookings (equipment,start_time,end_time,notes,selected_date,date_added) values ('".$equipment."','".$start."','".$end."','".$notes."','".$eventdate."',now())";

     $resultinsert=mysqli_query($dbconnection,$sqlinsert);

     if($resultinsert){
         echo "<span class='go'> &nbsp;Booking was successful&nbsp; </span>";
 }else{
         echo "Booking was not successful";
     }

 }
 ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Looking at the code, even if there is more than one result, the insert will always run – Carl Binalla Nov 03 '17 at 03:02
  • Hi. I managed to solve the problem- all I required was an else statement after the echo statement. It now works. Thanks to everyone for the help. –  Nov 03 '17 at 03:27

0 Answers0