0

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.

alogico
  • 1
  • 2
  • Your query is correct.With OR it would make no sense, as it will let a user to have only one booking of all time. You have to make sure that the input data is correct. – Your Common Sense Oct 18 '20 at 11:09
  • @YourCommonSense I found the error,this line: `$stmt->bindParam(":day",$post['date']);` should be just: `$stmt->bindParam(":day",$day);` . Thank you, I appreciate your help! – alogico Oct 20 '20 at 01:39

0 Answers0