简体   繁体   中英

How can I display the email of the user who has booked

I'm creating a small "time slot" booking application.

But I'm having some problems displaying which user, that has booked a specific timeslot.

The idea is, to have a link called Reserve, and when there are no bookings, a text saying: Booked by: [The users email]

The way is display my time links is like this:

$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 {
        echo  $user->getEmail();
        while ( $row = mysqli_fetch_array ( $result ) ) {
            echo "<br />";
        }
    }

As you can see, I echo my function $user->getEmail(); if there are any bookings.

I've tried to do this is my function.

public function getEmail() {
            $str = "SELECT bookings.*, users.email AS booking_email FROM bookings, users WHERE bookings.user_id = users.id";
            $result = $this->db->mysqli->query ($str);
            $string = "";
            while ( $row = $result->fetch_assoc () ) {
                $string .= $row ['booking_email'];
            }
            return $string;
        }

But as you would properbly imagine, that echoes out every single email in my database.

I¨m thinking that I need something more in my sql query?

You can see my foreign key relation here:

在此处输入图片说明

I hope some of you can point me in the right direction. And please tell me if I need to provide more code.

EDIT:

This is how I create my bookings:

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;
}

Please explicitly define your joins, instead of using implicit joins. It makes SQL easier to understand and avoids mistakes.

If you adjust your first SQL statement you can fetch the Users email address with the room availability if it is available.

SELECT
    `bookings`.*,
    `users`.`email` AS booking_email
FROM `bookings`
LEFT OUTER JOIN `users`
    ON (`bookings`.`user_id` = `users`.`id`)
WHERE
        `room_id`   = '".$room_id."'
    AND `eventDate` = '".$dateToCompare."'
    AND `timeslot`  = '".$time."'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM