问题:处理后未发布输入。我的假设是表单标记位置的放置无效,该查询能够从数据库中获取数据,但不能从用户输入中获取数据。尝试移动表单标签的位置,但无济于事。
输入代码:
<?php
session_start();
<?php
include './auth.php';
// check available room
$result = mysqli_query($conn,"SELECT r.room_id, (r.total_room-br.total) as availableroom from room as r LEFT JOIN (
SELECT roombook.room_id, sum(roombook.totalroombook) as total from roombook where roombook.booking_id IN
(
SELECT b.booking_id as bookingID from booking as b
where
(b.checkin_date between '".$datestart."' AND '".$dateend."')
OR
(b.checkout_date between '".$dateend."' AND '".$datestart."')
)
group by roombook.room_id
)
as br
ON r.room_id = br.room_id");
echo mysqli_error($conn);
if(mysqli_num_rows($result) > 0){
echo "<p><b>Choose Your Room</b></p><hr class=\"line\">";
the focus row print " <form action=\"guestform.php\" method=\"post\">\n";
while ($row = mysqli_fetch_array($result)) {
if($row['availableroom'] != null && $row['availableroom'] > 0 )
{
$sub_result = mysqli_query($conn,"select room.* from room where room.room_id = ".$row['room_id']." ");
if(mysqli_num_rows($sub_result) > 0)
{
while($sub_row = mysqli_fetch_array($sub_result)){
print " <p><h4>".$sub_row['room_name']."</h4></p>\n";
print " <div class=\"row\">\n";
print " \n";
print " <div class=\"large-4 columns\">\n";
print " <img src=\"".$sub_row['imgpath']."\"></img>\n";
print " <p><span class=\"fontgrey\">Occupancy : </span> ".$sub_row['occupancy']."<br>\n";
print " <span class=\"fontgrey\">Size : </span> ".$sub_row['size']."\n";
print " <br><span class=\"fontgrey\">View : </span> ".$sub_row['view']."</p>\n";
print " </div>\n";
print " <div class=\"large-4 columns\">\n";
print " the focus row <p ><span class=\"fontgrey\">Total Adult</span></p><input type=\"text\" name=\"total_adult\"><br>\n";
print " the focus row <p ><span class=\"fontgrey\">Total Children</span></p><input type=\"text\" name=\"total_children\" ><br>\n";
print "\n";
print " </div>\n";
print " <div class=\"large-4 columns\">\n";
print " the focus row <p ><span class=\"fontgrey\">Rate : MYR </span></p><input type=\"text\" name=\"rate\" value=\"".$sub_row['weekday']."\"><span class=\"fontgrey\">/person</span><br>\n";
print " the focus row <p ><span class=\"fontgrey\">Rate : MYR </span></p><input type=\"text\" name=\"chrate\" value=\"".$sub_row['cweekday']."\"><span class=\"fontgrey\">/person</span><br>\n";
print " <span style=\"text-align:right;\">".$row['availableroom']." room available</span>\n";
print " </p>\n";
print " <div class=\"row\">\n";
print " <div class=\"large-11 columns\">\n";
print " <label class=\"fontcolor\">\n";
print " <select class=\"no_of_room\" name=\"qtyroom".$sub_row['room_id']."\" id=\"room".$sub_row['room_id']."\" onChange=\"selection(".$sub_row['room_id'].")\" style=\"width:100%; color:black; height:45px;\" ;\">\n";
print " <option value=\"0\">0</option>\n";
$i = 1;
while($i <= $row['availableroom'])
{
print " <option value=\"".$i."\">".$i."</option>\n";
$i = $i+1;
}
print " </select>\n";
print " </label>\n";
print " </div>\n";
print " <div class=\"large-1 columns\">\n";
print "<input type=hidden name=\"selectedroom".$sub_row['room_id']."\" id=\"selectedroom".$sub_row['room_id']."\" value=\"".$sub_row['room_id']."\">";
print "<input type=hidden name=\"room_name".$sub_row['room_id']."\" id=\"room_name".$sub_row['room_id']."\" value=\"".$sub_row['room_name']."\">";
print " </div>\n";
print " </div>\n";
print " </div>\n";
print " \n";
print " </div>\n";
print " \n";
print " <hr>";
}
}
}
else if($row['availableroom'] == null ){
$sub_result2 = mysqli_query($conn,"select room.* from room where room.room_id = ".$row['room_id']." ");
if(mysqli_num_rows($sub_result2) > 0)
{
while($sub_row2 = mysqli_fetch_array($sub_result2)){
print " <p><h4>".$sub_row2['room_name']."</h4></p>\n";
print " <div class=\"row\">\n";
print " \n";
print " <div class=\"large-4 columns\">\n";
print " <img src=\"".$sub_row2['imgpath']."\"></img>\n";
print " <p><span class=\"fontgrey\">Occupancy : </span> ".$sub_row2['occupancy']."<br>\n";
print " <span class=\"fontgrey\">Size : </span> ".$sub_row2['size']."\n";
print " <br><span class=\"fontgrey\">View : </span> ".$sub_row2['view']."</p>\n";
print " </div>\n";
print " <div class=\"large-4 columns\">\n";
print " the focus row <p ><span class=\"fontgrey\">Total Adult</span></p><input type=\"text\" name=\"total_adult\"><br>\n";
print " the focus row <p ><span class=\"fontgrey\">Total Children</span></p><input type=\"text\" name=\"total_children\"><br>\n";
print "\n";
print " </div>\n";
print " <div class=\"large-4 columns\">\n";
print " the focus row <p ><span class=\"fontgrey\">Rate : MYR </span></p><input type=\"text\" name=\"rate\" value=\"".$sub_row2['weekday']."\"><span class=\"fontgrey\">/person</span><br>\n";
print " the focus row <p ><span class=\"fontgrey\">Rate : MYR </span></p><input type=\"text\" name=\"chrate\" value=\"".$sub_row2['cweekday']."\"><span class=\"fontgrey\">/person</span><br>\n";
print " <div><span style=\"text-align:right;\">".$sub_row2['total_room']." room available</span></div>\n";
print " </p>\n";
print " <div class=\"row\">\n";
print " <div class=\"large-11 columns\">\n";
print " <label class=\"fontcolor\">\n";
print " <select class=\"no_of_room\" name=\"qtyroom".$sub_row2['room_id']."\" id=\"room".$sub_row2['room_id']."\" onChange=\"selection(".$sub_row2['room_id'].")\" style=\"width:100%; color:black; height:45px;\" >\n";
print " <option value=\"0\">0</option>\n";
$i = 1;
while($i <= $sub_row2['total_room'])
{
print " <option value=\"".$i."\">".$i."</option>\n";
$i = $i+1;
}
print " </select>\n";
print " </label>\n";
print " </div>\n";
print " <div class=\"large-1 columns\">\n";
print "<input type hidden name=\"selectedroom".$sub_row2['room_id']."\" value=\"".$sub_row2['room_id']."\">";
print "<input type=hidden name=\"room_name".$sub_row2['room_id']."\" value=\"".$sub_row2['room_name']."\">";
//print " <button type=\"submit\" class=\"book button small\" style=\"background-color:#2ecc71; width:100%; height:45px; !important;\" >Book</button>\n";
print " </div>\n";
print " </div>\n";
print " </div>\n";
print " \n";
print " </div>\n";
print " \n";
print " <hr>";
}
}
}
}
the focus row print "<button type=\"submit\" id=\"submit-form\" class=\"hidden\" style=\"display:none\">Book</button>\n";
the focus row print " </form>";
}
?>
从上面的代码请找到输入应位于的“焦点行” 。
然后guestform.php
还包含您应重点关注的注释。
<?php
session_start();
$today = date("Y-m-d H:i:s");
include './auth.php';
if(!isset($_SESSION['room_id'])){
$_SESSION['room_id'] = array();
$_SESSION['roomname'] = array();
$_SESSION['roomqty'] = array();
$_SESSION['total_adult'] = array(); the focus row
$_SESSION['total_children'] = array(); the focus row
$_SESSION['rate'] = array(); the focus row
$_SESSION['chrate'] = array(); the focus row
$_SESSION['ind_rate'] = array();
$_SESSION['total_amount'] = 0;
$_SESSION['deposit'] = 0;
}
$result = mysqli_query($conn,"select * from room");
if(mysqli_num_rows($result) > 0){
$count = 0;
while($row = mysqli_fetch_array($result)){
if (isset($_POST["qtyroom".$row['room_id'].""]) && !empty($_POST["qtyroom".$row['room_id'].""]) )
{
$_SESSION['room_id'][$count] = $_POST["selectedroom".$row['room_id'].""];
$_SESSION['roomqty'][$count] = $_POST["qtyroom".$row['room_id'].""];
$_SESSION['roomname'][$count] = $_POST["room_name".$row['room_id'].""];
the focus row $_SESSION['total_adult'][$count] = $_POST["total_adult"];
the focus row $_SESSION['total_children'][$count] = $_POST["total_children"];
the focus row $_SESSION['rate'][$count] = $_POST["rate"];
the focus row $_SESSION['chrate'][$count] = $_POST["chrate"];
$_SESSION['ind_rate1'][$count] = ((int)$_SESSION['rate'][$count] * (int) $_SESSION['total_adult'][$count]);
$_SESSION['ind_rate2'][$count] = ((int)$_SESSION['chrate'][$count] * (int)$_SESSION['total_children'][$count]);
$_SESSION['total1'] = ( $_SESSION['ind_rate1'][$count]) ;
$_SESSION['total2'] = ( $_SESSION['ind_rate2'][$count]) ;
$_SESSION['total_amount'] = $_SESSION['ind_rate2'][$count]+ $_SESSION['ind_rate1'][$count] ;
$_SESSION['deposit'] = $_SESSION['total_amount'] * 0.15;
$count = $count + 1;
}
}
}
?>
查询
<?php
$_SESSION['booking_id'] = mysqli_insert_id($conn);
$count = 0;
foreach ($_SESSION['room_id'] as &$value0 ) {
$l="INSERT INTO roombook ( booking_id, room_id, totalroombook, total_adult, total_children, rate, chrate, id) VALUES ('".$_SESSION['booking_id']."','".$value0."','".$_SESSION['roomqty'][$count]."','".$_SESSION['total_adult'][$count]."','".$_SESSION['total_children'][$count]."','".$_SESSION['rate'][$count]."','".$_SESSION['chrate'][$count]."',NULL)";
mysqli_query($conn,$l);
$count = $count+1;
echo mysqli_error($conn);
print_r ($l);
}
?>
期望的查询返回:
INSERT INTO roombook ( booking_id, room_id, totalroombook, total_adult, total_children, rate, chrate, id) VALUES ('34','1','1','1','1','1','1',NULL)
在评论请求后改进
var_dump($ _ SESSION);
["total_night"]=> string(1) "1" ["room_id"]=> &array(1) { [0]=> &string(1) "1" } ["roomname"]=> array(1) { [0]=> string(19) "Standard Room(QUAD)" } ["roomqty"]=> array(1) { [0]=> string(1) "1" } ["total_adult"]=> array(1) { [0]=> string(0) "" } ["total_children"]=> array(1) { [0]=> string(0) "" } ["rate"]=> array(1) { [0]=> string(3) "499" } ["chrate"]=> array(1) { [0]=> string(3) "299" } ["ind_rate"]=> array(0) { } ["total_amount"]=> string(6) "845.88" ["deposit"]=> string(4) "1000" ["ind_rate1"]=> array(1) { [0]=> int(0) } ["ind_rate2"]=> array(1) { [0]=> int(0) } ["total1"]=> int(0) ["total2"]=> int(0) ["booking_id"]=> int(35)
答案 0 :(得分:2)
是的,表单标签的位置可能会影响从浏览器提交的数据。
但是,您的主要问题似乎是要遍历多个数据库结果行,并使用重复的name
属性在每行中创建表单元素。
重复的表单字段名称提交导致您提交的表单仅具有相同名称的 last 表单字段值。
例如:
<form method="post">
<input name="a" type="text" value="1"/>
<input name="a" type="text" value="2"/>
<button type="submit">Submit</button>
</form>
将导致PHP接收
$_POST = ['a' => 2]
这似乎在某些表单字段中得到了部分解决,但在其他表单字段中却没有添加房间ID作为后缀。 name=\"selectedroom".$sub_row['room_id']."\"
我建议更改您的表单元素名称,以便将其作为房间数组提交到并更改guestform.php
脚本以接受值数组。
<form>
<?php while ($row = mysqli_fetch_array($result)) {
<input name="room[<?php echo $row['room_id']; ?>][total_adult]"/>
<?php } ?>
<button type="submit">Submit</button
</form>
guestform.php 不再需要查询数据库。示例:https://3v4l.org/BZpEP
if (!empty($_POST['room'])) {
$rooms = array_flter((array) $_POST['room'], function($room) {
return !empty($room['qty']);
});
//...
$count = 0;
foreach ($rooms as $room_id => $room) {
$_SESSION['room_id'][$count] = $room_id;
$_SESSION['roomqty'][$count] = $room['qty'];
$_SESSION['roomname'][$count] = $room['name'];
$_SESSION['total_adult'][$count] = $room['total_adult'];
$_SESSION['total_children'][$count] = $room['total_children'];
$_SESSION['rate'][$count] = $room['rate'];
$_SESSION['chrate'][$count] = $room['chrate'];
//...
$count++;
}
}
但是,您的代码中也存在大量无效的HTML语法问题,这些问题会破坏遵循无效语法的HTML并导致浏览器出现渲染问题。这可能会导致JavaScript损坏,表单数据损坏或显示问题。
我建议将HTML和PHP代码分成简单的<div><?php echo $variable; ?></div>
样式格式。
这将使与HTML相关的问题的疑难解答更加容易,因为您不再需要跟踪是否已转义了哪些文本,并允许HTML和PHP语法突出显示器(如StackOverflow或PHPStorm)来解析代码。
在HTML中发现的问题列表。
<p>
元素不能包装<h4>
和<div>
元素。<p>
个元素通常不允许包含其他块 其中的元素。
<img>
元素不使用结束标记</img>
。HTML属性未正确关闭
<select ... style=\"width:100%; color:black; height:45px;\" ;\">\n"
导致<select style="";">
关闭
</p>
元素缺少开始标记
我根据建议更新了您的代码,并使用<!-- -->
注释了无效的HTML代码,以便于参考。
<?php
session_start();
require_once __DIR__ . '/auth.php';
// check available room
$result = mysqli_query($conn, "SELECT r.room_id, (r.total_room-br.total) AS availableroom
FROM room AS r
LEFT JOIN (
SELECT roombook.room_id, SUM(roombook.totalroombook) AS total
FROM roombook
WHERE roombook.booking_id IN(
SELECT b.booking_id as bookingID
FROM booking AS b
WHERE
(b.checkin_date between '" . $datestart . "' AND '" . $dateend . "')
OR
(b.checkout_date between '" . $dateend . "' AND '" . $datestart . "')
)
GROUP BY roombook.room_id
) AS br
ON r.room_id = br.room_id");
if (mysqli_num_rows($result) > 0) { ?>
<p>
<b>Choose Your Room</b>
</p>
<hr class="line">
<form action="guestform.php" method="post">
<?php while ($row = mysqli_fetch_array($result)) {
if ($row['availableroom'] != null && $row['availableroom'] > 0) {
$sub_result = mysqli_query($conn, 'select room.* from room where room.room_id = ' . $row['room_id'] . ' ');
if (mysqli_num_rows($sub_result) > 0) {
while ($sub_row = mysqli_fetch_array($sub_result)) { ?>
<!-- <p> --><h4><?php echo $sub_row['room_name']; ?></h4><!-- </p> -->
<div class="row">
<div class="large-4 columns">
<img src="<?php echo $sub_row['imgpath']; ?>"><!-- </img> -->
<p>
<span class="fontgrey">Occupancy : </span> <?php echo $sub_row['occupancy']; ?><br>
<span class="fontgrey">Size : </span> <?php echo $sub_row['size']; ?><br>
<span class="fontgrey">View : </span> <?php echo $sub_row['view']; ?>
</p>
</div>
<div class="large-4 columns">
<p>
<span class="fontgrey">Total Adult</span>
</p>
<input type="text" name="room[<?php echo $sub_row['room_id']; ?>][total_adult]"><br>
<p>
<span class="fontgrey">Total Children</span>
</p>
<input type="text" name="room[<?php echo $sub_row['room_id']; ?>][total_children]"><br>
</div>
<div class="large-4 columns">
<p>
<span class="fontgrey">Rate : MYR </span>
</p>
<input type="text" name="room[<?php echo $sub_row['room_id']; ?>][rate]" value="<?php echo $sub_row['weekday']; ?>"><span class="fontgrey">/person</span><br>
<p>
<span class="fontgrey">Rate : MYR </span>
</p>
<p> <!-- added p -->
<input type="text" name="room[<?php echo $sub_row['room_id']; ?>][chrate]" value="<?php echo $sub_row['cweekday']; ?>"><span class="fontgrey">/person</span><br>
<span style="text-align:right;"><?php echo $row['availableroom']; ?> room available</span>
</p>
<div class="row">
<div class="large-11 columns">
<label class="fontcolor">
<select class="no_of_room" name="room[<?php echo $sub_row['room_id']; ?>][qty]" id="room<?php echo $sub_row['room_id']; ?>" onChange="selection(<?php echo $sub_row['room_id']; ?>)" style="width:100%; color:black; height:45px;" <!-- ;" -->>
<?php for ($i = 0; $i <= $row['availableroom']; $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</label>
</div>
<div class="large-1 columns">
<input type=hidden name="room[<?php echo $sub_row['room_id']; ?>][name]" id="room_name<?php echo $sub_row['room_id']; ?>" value="<?php echo $sub_row['room_name']; ?>">
</div>
</div>
</div>
</div>
<hr>
<?php }
}
} elseif ($row['availableroom'] == null) {
$sub_result2 = mysqli_query($conn, 'select room.* from room where room.room_id = "' . $row['room_id'] . '"');
if (mysqli_num_rows($sub_result2) > 0) {
while ($sub_row2 = mysqli_fetch_array($sub_result2)) { ?>
<!-- <p> --><h4><?php echo $sub_row2['room_name']; ?></h4><!-- </p> -->
<div class="row">
<div class="large-4 columns">
<img src="<?php echo $sub_row2['imgpath']; ?>"><!-- </img> -->
<p>
<span class="fontgrey">Occupancy : </span> <?php echo $sub_row2['occupancy']; ?><br>
<span class="fontgrey">Size : </span> <?php echo $sub_row2['size']; ?><br>
<span class="fontgrey">View : </span> <?php echo $sub_row2['view']; ?>
</p>
</div>
<div class="large-4 columns">
<p>
<span class="fontgrey">Total Adult</span>
</p>
<input type="text" name="room[<?php echo $sub_row2['room_id']; ?>][total_adult]"><br>
<p>
<span class="fontgrey">Total Children</span>
</p>
<input type="text" name="room[<?php echo $sub_row2['room_id']; ?>][total_children]"><br>
</div>
<div class="large-4 columns">
<p>
<span class="fontgrey">Rate : MYR </span>
</p>
<input type="text" name="room[<?php echo $sub_row2['room_id']; ?>][rate]" value="<?php echo $sub_row2['weekday']; ?>"><span class="fontgrey">/person</span><br>
<p>
<span class="fontgrey">Rate : MYR </span>
</p>
<p> <!-- added p -->
<input type="text" name="room[<?php echo $sub_row2['room_id']; ?>][chrate]" value="<?php echo $sub_row2['cweekday']; ?>"><span class="fontgrey">/person</span><br>
<!-- <div> --> <span style="text-align:right;"><?php echo $sub_row2['total_room']; ?> room available</span><!-- </div> -->
</p>
<div class="row">
<div class="large-11 columns">
<label class="fontcolor">
<select class="no_of_room" name="room[<?php echo $sub_row2['room_id']; ?>][qty]" id="room<?php echo $sub_row2['room_id']; ?>" onChange="selection(<?php echo $sub_row2['room_id']; ?>)" style="width:100%; color:black; height:45px;">
<?php for ($i = 0; $i <= $sub_row2['total_room']; $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</label>
</div>
<div class="large-1 columns">
<input type=hidden name="room[<?php echo $sub_row2['room_id']; ?>][name]" value="<?php echo $sub_row2['room_name']; ?>">
</div>
</div>
</div>
</div>
<hr>
<?php }
}
}
} ?>
<button type="submit" id="submit-form" class="hidden" style="display:none">Book</button>
</form>
<?php }
答案 1 :(得分:0)
这是更新
主要问题的答案通常是肯定的
例如
请勿在任何输入行之前打印结束标记
print "<form action="" method ="post">";
if(isset ($_SESSION[something])) && ($_SESSION[$something] >0{
if ($_SESSION[something] > 10) {
print" <input type=\"text\" name=\"name\">";
}
print" </form>";
}
print" <input type=\"text\" name=\"number\">";
相反,
print "<form action="" method ="post">";
if(isset ($_SESSION[something])) && ($_SESSION[$something] >0{
if ($_SESSION[something] > 10) {
print" <input type=\"text\" name=\"name\">";
}
}
print" <input type=\"text\" name=\"number\">";
print" </form>";
但是对于此特定代码,表单标签是正确的,但是问题出在输入行和过程代码之内。
输入行
<input type=\"text\" name=\"total_adult".$sub_row['room_id']."\">
<input type=\"text\" name=\"total_children".$sub_row['room_id']."\" >
<input type=\"text\" name=\"rate".$sub_row['room_id']."\" value=\"".$sub_row2['weekday']."\">
<input type=\"text\" name=\"chrate".$sub_row['room_id']."\" value=\"".$sub_row2['cweekday']."\">
流程代码
$_SESSION['total_adult'][$count] = $_POST["total_adult".$row['room_id'].""];
$_SESSION['total_children'][$count] = $_POST["total_children".$row['room_id'].""];
$_SESSION['rate'][$count] = $_POST["rate".$row['room_id'].""];
$_SESSION['chrate'][$count] = $_POST["chrate".$row['room_id'].""];