1

我在第一篇文章中并不清楚。所以就在这里。

预订.php

<table style="border: 4px;">

    <tr>
                    <!-- Start JQuery Pop-up -->

        <script src="js/jquery.js" type="text/javascript"></script>
        <script src="popup2.js" type="text/javascript"></script>

        <td style="border: 2px solid #000;"><center><img src="images/comp.jpg" height="30" width="50"><br /><b><div id="button"><a href="#" class="pcbutton">PC 05</a></div></b></center>

        </td>


    <!-- Jquery Pop-Up code starts here including the insert query -->
            <?php
                echo '<div id="popupContact">';
                echo '<a id="popupContactClose">x</a>';
                echo '<center><form method = "POST" action="reserve.php">';

                                        mysql_connect("localhost","root","") or die (mysql_error());
                                            mysql_select_db('seat_reservation') or die (mysql_error());

                                            $roomNumber = '400';
                                            $chairNumber = 5;

    //In this part, I try to insert in the database without submitting the form yet.

                                            $insert = mysql_query("INSERT INTO reservation (chair_status, room_id, chair_number) VALUES (0, '$roomNumber', '$chairNumber')");



    //I'm trying to retrieve the inserted data and do the checking.

                                    $query = mysql_query("SELECT * FROM reservation WHERE room_id = '$roomNumber' AND chair_number = '$chairNumber'");
                                            $row = mysql_fetch_assoc($query);

                                            $_SESSION['roomno'] = $row['room_id'];
                                            $_SESSION['chairnum'] = $row['chair_number'];

                                            echo '<h1>Information </h1>';
                                                echo 'Fullname : <input type="text" name="student_name" /><br /><br />';
                                                echo 'Student ID No. : <input type="text" name="stud_id" />';
                                                echo '<br /><br />';
                                                echo '<input type="submit"  value="Submit" />';
                                            echo '</form></center>';
                                        echo '</div>';

                                        echo '<div id="backgroundPopup"></div>';
                                        ?>
                                    <!-- End JQuery Pop-up -->  
                                        <td style="border: 2px solid #000;"><center><img src="images/comp.jpg" height="30" width="50"><br /><div id="button2"><a href="#" class="pcbutton"><b>PC 04</b></a></div></center>
                                        </td>
    //This is another table that's exactly the same from the code above only that this has a different PC number
                                        <?php
                                        echo '<div id="popupContact">';
                                            echo '<a id="popupContactClose">x</a>';
                                            echo '<center><form method = "POST" action="reserve.php">';

                                            mysql_connect("localhost","root","") or die (mysql_error());
                                            mysql_select_db('seat_reservation') or die (mysql_error());

                                            $roomNumber = '400';
                                            $chairNumber = 4;

                                            $insert = mysql_query("INSERT INTO reservation (chair_status, room_id, chair_number) VALUES (0, '$roomNumber', '$chairNumber')");

                                            $query = mysql_query("SELECT * FROM reservation WHERE room_id = '$roomNumber' AND chair_number = '$chairNumber'");
                                            $row = mysql_fetch_assoc($query);

                                            $_SESSION['roomno'] = $row['room_id'];
                                            $_SESSION['chairnum'] = $row['chair_number'];

                                            echo '<h1>Information </h1>';
                                                echo 'Fullname : <input type="text" name="student_name" /><br /><br />';
                                                echo 'Student ID No. : <input type="text" name="stud_id" />';
                                                echo '<br /><br />';
                                                echo '<input type="submit"  value="Submit" />';
                                            echo '</form></center>';
                                        echo '</div>';
                                        echo '<div id="backgroundPopup"></div>';
                                        ?>

保留.php

<?php
    session_start();

    $name = $_POST['student_name'];
    $stud_id = $_POST['stud_id'];
    $room_id = $_SESSION['roomno'];
    $chair_num = $_SESSION['chairnum'];

    mysql_connect("localhost", "root", "") or die (mysql_error());
    mysql_select_db('seat_reservation') or die (mysql_error());

    $query = mysql_query("SELECT * FROM reservation WHERE room_id = '$room_id' AND chair_number = '$chair_num'");
    $row = mysql_fetch_assoc($query);

    if($row['chair_status'] == 0)
    {
    $insert = mysql_query("UPDATE reservation SET chair_status = 1, student_name = '$name', stud_id = '$stud_id' WHERE room_id = '$room_id' AND chair_number = '$chair_num'");
        if($insert)
            die ("Seat reserved! <br />Redirecting...<meta http-equiv='refresh' content=2;reservation.php>");
        else
            die ("Try Again! <br />Redirecting...<meta http-equiv='refresh' content=2;reservation.php>");
    }

    else
        die ("Sorry, seat taken! <br />Redirecting...<meta http-equiv='refresh' content=2;reservation.php>");
    ?>

以下是座位预订截图:

链接:[注意:将 [DOT] 更改为 .] https://fbcdn-sphotos-a.akamaihd[DOT]net/hphotos-ak-ash3/599637_4018881323775_858410964_n.jpg

这是单击 PC 时弹出的屏幕截图:

链接:[注意:将 [DOT] 更改为 .] https://fbcdn-sphotos-a.akamaihd[DOT]net/hphotos-ak-ash3/599637_4018881323775_858410964_n.jpg

问题:应用插入代码仅适用于一台 PC,例如 PC 05。在上面的代码中,我将插入代码应用于 PC 05 和 PC 04,仅作为测试,但我想将其应用于所有 PC。但是仅在 PC 05 和 PC 04 中应用它并不能使其工作。在数据库中,单击提交按钮后,仅插入以下内容(注意:room_id 是房间号):

链接:https ://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash3/527207_4018929644983_1759106011_n.jpg

每个学生都应该在这个特定的教室里预留一个座位。当 Student.A 点击 PC 05 作为他的座位时,应该为他保留它。当 Student.B 再次单击 PC 01 作为他的座位时,它应该为他保留。有多个教室,为此我使用了 JQuery 选项卡。我想在数据库中拥有的是这个(注意:仅限手动插入):

链接:https ://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/376602_4018990806512_1150698954_n.jpg

您的回复将不胜感激,在此先感谢您!

4

2 回答 2

0

你的代码有点不完整。不要单独使用mysql_query。包括额外的代码来捕获错误或在插入数据时发生错误时显示输出。

$insert = mysql_query("INSERT INTO reservation (chair_status, room_id, chair_number) VALUES (0, 400, 5)");

// I added the below lines to catch how many rows were inserted and if not, what error occured
$inserted_rows = mysql_affected_rows(); 

if ($inserted_rows !=0)
  echo 'Error occurred, through out error'. mysql_error();
于 2012-07-07T13:52:11.017 回答
0

首先为您的提交按钮命名,以便您可以使用它来检查表单是否已提交:

if($_POST['name_of_your_submit_button']){
  //mysql insert code here
}

而且我看不出您是否真的在插入查询中使用了用户输入的数据:

$insert = mysql_query("INSERT INTO reservation (chair_status, room_id, chair_number) VALUES (0, 400, 5)");

确保通过使用诸如 print_r 或 var_dump 之类的调试代码来获取一些值

print_r($_POST);
于 2012-07-07T14:16:19.220 回答