I have a calendar where I can pick a date, and then a table of timeslots will appear. In the table I can either see information about the user who has booked the timeslot, or click a reserve link.
To display the information, I check select all records from my booking table where the room id is equal to the current room that I am booking for, the eventdate, that is equal to the date that I clicked in the calender and the timeslot which it equal to the timeslot that I clicked on:
$sql = "SELECT * FROM bookings WHERE room_id= '" . $room_id . "' AND eventDate='" . $dateToCompare . "'AND timeslot = $time;";
$result = $db->mysqli->query ( $sql );
if (mysqli_num_rows ( $result ) == 0) {
echo "<a href='" . $_SERVER ['PHP_SELF'] . "?id=" . $room_id . "&month=" . $month . "&day=" . $day . "&year=" . $year . "&t={$time}&v=true&f=true&reserved=true'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
}
else { .... }
As you can see I set put reserved=true in my Reserve link, and by calling that I insert into my database.
if (isset ( $_GET ['reserved'] )) {
$sqlreserve = "INSERT INTO bookings (user_id, room_id, eventDate, timeslot) VALUES ('" . $user_id . "','" . $room_id . "','" . $dateToCompare . "','" . intval ( $_GET ['t'] ) . "');";
$resultreserve = $db->mysqli->query ( $sqlreserve );
echo $resultreserve;
}
I would end up with a link looking something like this:
mysite.php/id=2&month=04&day=23&year=2014&t=12&v=true&f=true&reserved=true
However, this is where I have my problem. If I refresh my page, a booking will be made again. I'm a bit lost on what the solution would be, and that is why I'm asking you guys.
How can a restrick a specific timeslot on a specific day, to only be booked once?
A booking would look like this in my database:
user_id - room_id - eventDate - timeslot
28 3 04/18/2014 8
The eventdate and the timeslot should not be UNIQUE separately. A time slot could have the value of 10, 200 times fx. But the eventdate and the timeslot should be UNIQUE togeather.
Is this possible?
EDIT:
I tried to add this:
ALTER TABLE `exam`.`bookings` ADD UNIQUE `idx_booking` (`eventDate`, `timeslot`);
And it almost works. However, I can't delete the timeslot that I reserved last. All of the others I can delete.
My delete looks like this:
if ($booking->deleteBooking($_GET['id']))
{
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else
{
echo "Could not delete the booking!";
}
public function deleteBooking($id)
{
$sql = "DELETE FROM bookings WHERE id = ?";
if (!$result = $this->db->mysqli->prepare($sql))
{
return false;
}
if (!$result->bind_param('i', $id))
{
return false;
}
return $result->execute();
}
I've found the error!
Each time I deleted the last booking, it wan't back and booked that timeslot again.