I'm developing a booking system with PDO/MySql where the user can book a service only once for the same day.Therefore I did:
$day=$_POST['date'];
$slot=$_POST['hour'];
$uname="Jamie Doe";
$sql ="SELECT * FROM `Prenotazioni` WHERE username= :uname AND giorno= :day";
$stmt=$PDOconn->prepare($sql);
$stmt->bindParam(":uname",$uname);
$stmt->bindParam(":day",$post['date']);
$stmt->execute();
if($stmt->rowCount() >= 1){
echo "Note: You already booked for today,let's try for another day";
}else{
try{
$sql="INSERT INTO Prenotazioni (giorno,classe_orario,username) VALUES (?,?,?)";
$PDOconn->prepare($sql)->execute([$day,$slot,$uname]);
}catch(PDOException $e){
$ec=$e->getMessage();
}
//check here date format with echo $day;
}
}
but it doesn't work, meaning the same user is able to book multiple time-slots for the same day(at least this show me that variable are instantiated the right way). But 1)I tried the same sql line in phpmyadmin where the database is and it works, i got back lines where a single username has booked in the given day. 2) Just to check my code i tried OR instead of AND .... and it worked, username can book only once and there is only one booking per day possible!
I also tried this solution but it didn't worked: MySQL 'WHERE AND' issue
I can tell where my mistake is, in the PDO or in the MySql part. Can somebody please help me out? Thank you in advance for your time.