gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:29:40 26 4
gpt4 key购买 nike

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

预订.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

非常感谢您的回复,提前致谢!

最佳答案

您的代码有点不完整。不要单独使用 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();

关于php - 无法在数据库中插入必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11375461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com