Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AwadGorg's avatar

problem with booking system

Hello, I am creating a booking system and I get stuck on this issue when the user book for an appointment on a doctor he/she can cancel there appointment and am using this code for that

private function bookedCell($date)
    {
      $db = DB();
      $user_id = $_SESSION['id'];
      // check if this user made the booking
      $query = $db->prepare('SELECT id FROM bookings WHERE student_id = :id');
      $query->bindParam('id', $user_id, PDO::PARAM_INT);
      $query->execute();
      if ($query->rowCount() > 0) {
        $res = $query->fetchAll();
        return '<div class="booked">' . $this->deleteForm($this->bookingId($date)) . '</div>';
      }
    }

I want to show the cancel button to the users who made the booking but now the delete button is shown for everyone

0 likes
3 replies
Sinnbeck's avatar

I assume that you arent using laravel?

Do you mean that every user can delete their own booking? Cause as I can see from you code, you just check if there is a booking by that user and show a delete button. That seems correct I would assume?

aleahy's avatar

You would need to refer to the booking in your query. Your select query only checks if the user0 has ever made any booking.

If you're using laravel, wouldn't you have models with relationships set up? Then you could do something like:

if ($booking->user->is(auth()->user()) {
	return '<div class="booked">' . $this->deleteForm($this->bookingId($date)) . '</div>';
}

but the function would need the instance of the booking model. And if you're using blade, then this could be directly in the blade view.

SilenceBringer's avatar

@awadgorg currently your query

 $query = $db->prepare('SELECT id FROM bookings WHERE student_id = :id');

check that user has ANY booking in the system. I think you need to check by date to ($date passed as attribute, but you don't check that booking is for THIS specified date)

Please or to participate in this conversation.