我需要从数组(表)向数据库添加数据。但是这个POST.php无法正常工作。所以你们可以给我任何解决方案。
这是它给出的错误。
非法字符串偏移' h_name'在第11行的C:\ wamp \ www \ confirm \ post.php
第11行是:VALUES(' $ row [h_name]',' $ row [room]',' $ row [nors]' ,' $ row [nights]',' $ row [euro]',' $ row [date]',' 09090&#39 ;)&#34 ;;
所有插入的值都会给出错误。数组到字符串转换 谢谢。
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr ><td>Hotel Name</td><td>Room Type</td><td>Number of Rooms</td><td>Nights</td><td>EURO</td><td>Date</td></tr>
<tr><td><?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[h_name]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';/
?>
</td>
<td>
<?php echo '
<select name="list[room]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[nors]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" nname="list[nights]" placeholder="Nights"></td>
<td><input type="text" name="list[euro]" placeholder="euro"></td>
<td><input type="date" name="list[date]" placeholder="Date"></td>
<td><pre> </pre></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr><td>Total</td><td></td><td></td><td><input name="result" id="result"></td><td></td><td></td></tr>
</table>
</div>
</br>
<input type="submit" id="submit" name="submit" value="Register" color="red" style="width: 77px; height: 50px"></div>
</form>
post.php(php process)
<?php
include "conn.php";
print_r($_POST['list']);
foreach ($_POST as $row) {
$query = "INSERT INTO reqhotels (reqh_h_name, reqh_rtype, reqh_nor, reqh_nights, reqh_euro, reqh_date, reqh_req_no)
VALUES ('$row[h_name]', '$row[room]', '$row[nors]', '$row[nights]', '$row[euro]', '$row[date]', '09090')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
if($result > 0) {
echo"successfull";
}
else {
echo"fail";
}
}
?>
编辑:从回答问题移植到:Aug.05
这是我的HTML代码。 (html表)。在这里使用jquery为此表添加一些额外的行。所以我想将所有数据插入数据库。
表格列: rq_h_name,rq_rtype,rq_nors,rq_nights,rq_euro,rq_date
<form action="post.php" method="POST" id="register-form" novalidate="novalidate">
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr >
<td>Hotel Name</td>
<td>Room Type</td>
<td>Number of Rooms</td>
<td>Nights</td>
<td>EURO</td>
<td>Date</td>
</tr>
<tr>
<td>
<?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[0][]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';
?>
</td>
<td>
<?php echo '
<select name="list[0][]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[0][]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" name="list[0][]" placeholder="Nights"></td>
<td><input type="text" name="list[0][]" placeholder="euro"></td>
<td><input type="date" name="list[0][]" placeholder="Date"></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr>
<td>Total</td>
<td><input name="result" id="result"></td>
</tr>
</table>
</div>
</br>
答案 0 :(得分:0)
可能您想要执行以下操作
foreach ($_POST['list'] as $row) {
或类似而非当前
foreach ($_POST as $row) {
以您当前的方式,您在数组中有一个数组,因此该数组无法直接替换为字符串。我不确定这是否真的有效,但这就是你应该遵循的想法,并显示你遇到的问题。你的代码有点乱,所以我不能保证你没有其他错误。