简体   繁体   中英

PHP booking system

I am developing a booking system. The way my website works at the moment is the user logs in and selects an appointment. I need help in displaying that appointment they have booked on the next page. Also when they book the appointment, it should no longer be available to other users.

<?php
{
    mysql_connect("localhost" , "" , "") or die (mysql_error());
    mysql_select_db("") or die(mysql_error());


    $pid=intval($_SESSION["Patient_id"]); $query = "SELECT t1.*, t2.Doctor_name, t2.Doctor_room FROM Appointment AS t1 INNER JOIN Doctor AS t2 ON t1.Doctor_id=t2.Doctor_id";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");

    //this selects the results as rows
    $num = mysql_num_rows ($result);    


    while($row=mysql_fetch_assoc($result))
    {
        $_SESSION['Appointment_date'] = $row['Appointment_date'];
        $_SESSION['Appointment_time'] = $row['Appointment_time'];
        $_SESSION['Doctor_name'] = $row['Doctor_name'];
        $_SESSION['Doctor_room'] = $row['Doctor_room'];
    }
}
?>  

        <strong>Dates available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select date--</option>
        <option value="3"><?php echo $_SESSION['Appointment_date'];?></option>
        </select>

        <br />
        <br />

        <strong>Times available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select time--</option>
        <option value="3"><?php echo $_SESSION['Appointment_time'];?></option>>
        </select>

        <br />
        <br />

            <strong>Doctor Name</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Name--</option>
        <option value="2"><?php echo $_SESSION['Doctor_name'];?></option>>
        </select>

        <br />
        <br />

            <strong>Doctor Room</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Room--</option>
        <option value="2"><?php echo $_SESSION['Doctor_room'];?></option>>
        </select> 

The code above allows users to make appointments from what is available. This table is called the appointment table. When the user clicks next, i need this information to display back to the user so it displays their appointment and the appointment should no longer be available to other users

Thanks!

Firstly, the code you have doesn't work like you want it to. Judging from the fact that those are dropdowns, you're probably wanting multiple options in there. But what you're putting in is exactly one option: the very last row provided by the query. Your while loop is merely overwriting each row's information with the next until it exits.

And is there any reason you are using $_SESSION for these? Why not just make new variables to use? If you use $_SESSION, you are using the server's space to hold that information for the entire time the user is on your site, and then for however long after that it takes for your garbage collector to come and get it. If you only want the information for one page, you should use plain variables that will not remain saved and taking up space. (Not that a few lines of text take up that much space, but the principle of the thing....) If you are trying to avoid doing multiple database calls by saving to SESSION variables, you should be checking whether those variables are set first.

Secondly, your splitting of date and time into separate selectors means the users would be able to select date and time options that aren't actually available. For example, if you had 10:00 Tuesday and 4:00 Wednesday available, then by your intended design, your user would be able to select 10:00 Tuesday, 4:00 Tuesday, 10:00 Wednesday, or 4:00 Wednesday. The same actually goes for the different doctors and rooms, too, now that I think of it. Unless you're planning to book Doctor X in both Room A and Room B at the exact same time. You really can have only one dropdown unless you're going to use javascript and/or ajax, or reload the page and do a new database search, to change the available selections as the user selects them. For example, you'd probably want them to select a date, and then have the page automatically update what times and doctors are available, right? But that's a totally more complicated question and answer, so I'm leaving that alone for now.

Third, as other people already commented, make sure you get rid of that mysql_* stuff and replace it with modern methods. I can attest from experience that it can cause problems with database connections not closing as expected. I left it in inside my example below because you have three options for upgrading, and they all look different, so better not to confuse that issue just here.

Finally, to answer your question, you need to put your html inside a form, and also put a submit button inside the form so they can send it. In the form tag, you put the "action" as the url of the intended landing page, and the "method" as post. Then when the landing page loads, the information selected in the form will be inside the $_POST variable, according to the names of the elements that were in the form and the values of the options. This means you have to assign a different name to each element and you have to assign the correct values to the "value" of each option. And BTW, you can never have identical "id" elements on the same page.

Here is an example combining most of the things I just mentioned, although the dropdowns won't really work the way you need -- I kept three of them in order so you could see how POST works, but really you only want one dropdown.

//on your current page 
//.... database stuff .....
$dates_and_times = array();
$doctors = array();
$rooms = array();
while($row=mysql_fetch_assoc($result))
{
    $dates_and_times[] = $row['Appointment_date'].' '.$row['Appointment_time'];
    $doctors[] = $row['Doctor_name'];
    $rooms[] = $row['Doctor_room'];

    //with SESSION, this would have been:
    //(note the extra brackets [] to create arrays)
    //$_SESSION['Appointment_date_and_time'][] = $row['Appointment_date'].' '.$row['Appointment_time'];
    //$_SESSION['Doctor_name'][] = $row['Doctor_name'];
    //$_SESSION['Doctor_room'][] = $row['Doctor_room'];
}
?>
<form method="post" action="landing_page.php">
    <strong>Dates available</strong>            
    <select id="Date" name="Date">                      
    <option value="0">--Select date--</option>
    <?php  foreach ($dates_and_times as $app_date) { ?>
        <option value="<?php echo $app_date; ?>"><?php echo $app_date; ?></option>
    <?php } ?>
    </select>

    <br />
    <br />

        <strong>Doctor Name</strong>            
    <select id="Doctor" name="Doctor">                      
    <option value="0">--Name--</option>
    <?php  foreach ($doctors as $doc) { ?>
        <option value="<?php echo $doc; ?>"><?php echo $doc; ?></option>
    <?php } ?>
    </select>

    <br />
    <br />

        <strong>Doctor Room</strong>            
    <select id="Room" name="Room">                      
    <option value="0">--Room--</option>
    <?php  foreach ($rooms as $room) { ?>
        <option value="<?php echo $room; ?>"><?php echo $room; ?></option>
    <?php } ?>
    </select> 

    <input type="submit" name="Go" />
</form>


//on the landing page
//echo based on the name of the posted element
//$_POST has the values of all of the elements in the form that had a name
<strong> You selected an appointment with 
     <?php echo $_POST['Doctor']; ?>
     on 
     <?php echo $_POST['Date']; ?>
     in room
     <?php echo $_POST['Room']; ?>
</strong>

In addition to just displaying the information, you need to have something on the landing page to save the user's choice in the database. If you don't want other users to be able to book the same appointment time, that means that your original select statement needs to look inside the "booked appointments" table and find only appointment dates, times, doctors, and rooms that aren't already taken to display in the dropdowns. You will need to look into changing your current SELECT to only get available appointments, and you will need to look into INSERT queries to save the selected appointment using the POST information.

Answering the question in the comment: There are lots of different ways to design what you're looking for. I think you will have some users who are looking for a specific doctor, but other users who are looking for a particular time and don't care what doctor they get. That is why, if it were my website, I would make a selector based on day. I would put "next day" and "previous day" buttons at the top, as well as a calendar selector. When the page reloaded after they chose a day, I would then get the information of doctors and times from the database for only that day. With that information, I would create a big table with doctors on one axis and times on another. I would put buttons on the table for each available appointment time, so that the user would click that time x doctor to select their appointment, and obviously no button if that time x doctor were not available. Then users can see at a glance what times are available and what doctors are available for the entire day.

In my opinion, the best way to handle the page reloading stuff is to put the date selector inside its own same-page-posting form:

<form action="" method="get">

     //date selector here, named "Date"

</form>

I would then check the $_GET variable for whether a date has been selected. If so, I would load the table inside a second form. I would put the $_GET date inside a hidden input so that when they post something from that form, it gets sent along.

<form action="the_landing_page.php" method="post">
     <input type="hidden" name="Date" value="<?php echo $_GET['Date']; ?>" />

     //table here
     //submit button here

</form>

The user's selection from the table would then post to the_landing_page, just as in my first example. Now you get to figure out the details yourself. Good luck.

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