我试过在互联网上查找并使用一些问题中提供的答案,但无法使其发挥作用。如何防止PHP将重复记录插入数据库?我似乎可以在插入重复时显示“no”的echo语句,但无法阻止将实际的重复记录插入到数据库中。
我几乎尝试了一切,但没有任何工作。感谢任何可以提供帮助的人。
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'> Booking was successful </span>";
}else{
echo "Booking was not successful";
}
}
?>