无法在数据库中插入必填字段

时间:2012-07-07 13:35:35

标签: php mysql database insert

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

reservation.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>';
                                        ?>

reserve.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>");
    ?>

以下是座位预订的屏幕截图:

LINK:[注意:将[DOT]更改为。] HTTPS://fbcdn-sphotos-a.akamaihd [点]净/ hphotos-AK-AsH 3 / 599637_4018881323775_858410964_n.jpg

以下是点击PC时弹出窗口的屏幕截图:

LINK:[注意:将[DOT]更改为。] HTTPS://fbcdn-sphotos-a.akamaihd [点]净/ hphotos-AK-AsH 3 / 599637_4018881323775_858410964_n.jpg

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

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

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

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

您的回复将受到高度赞赏,请提前感谢您!

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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);