您好我想提供此信息以从select
插入多个记录<?php
include_once("connect.inc.php");
if (isset($_POST['submit'])) {
$province_id = $_POST['province_id'][$i];
$province_id = $_POST['travel_id'][$i];
$i = 0;
foreach ( $_POST as $val) {
mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="js/bootstrap.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="respond.js"></script>
<form action="index.php" method="post"> <!--ส่งค่า post ไปหน้าเดิม -->
<table width="896" border="1">
<thead>
<tr>
<th width="239">จังหวัด</th>
<th width="552">สถานที่ท่องเที่ยว</th>
<th width="83"><input type="hidden" id="txtNum" value="1" size="2" /></th>
</tr>
<tr>
<th width="239">
<select id="selProvince" name="province_id[]"> <!--Default จังหวััด-->
<option value="">กรุณาเลือกจังหวัด</option>
<?PHP
$SelectPr="SELECT * FROM province";
$QueryPro=mysql_query($SelectPr);
while($Pro=mysql_fetch_array($QueryPro)){
?>
<option value="<?=$Pro['province_id']?>"><?=$Pro['province_name']?></option>
<?PHP } ?>
</select>
</th>
<th width="552"><select name ="travel_id[]" id="selTravel"><option value="">กรุณาเลือกจังหวัด</option></select></th>
<th width="83"><button type="button" id="btnP">เพิ่มรายการ</button></th>
</tr>
<tr><td colspan="3"><center>รายการที่เพิ่ม</center></td></tr>
</thead>
<tbody>
</tbody>
</table>
<input name="submit" type="submit" value="add">
</form>
</body>
</html>
我插入1条记录,但我只选择一条记录,但会将更多数据导入第五条数据。
请帮助我,我解决了这个不会再来三天了。 请帮助我项目BA时间剩余1天
答案 0 :(得分:0)
您在循环中递增计数器 但只获得第一个值 - 在循环之外
修复变量中的错误
$province_id = $_POST['province_id'][$i];
$travel_id = $_POST['travel_id'][$i];
删除循环和引号
mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ($province_id, $travel_id)");
编辑 - 它应该看起来像这个
<?php
include_once("connect.inc.php");
if (isset($_POST['submit'])) {
$province_id = $_POST['province_id'];
$travel_id = $_POST['travel_id'];
mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ($province_id, $travel_id)");
}
?>
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试迭代$ _POST [&#39;省id&#39;],否则你运行循环遍历整个$ _POST数据,其中还包括隐藏字段,提交按钮等...
foreach ( $_POST['province_id'] as $province_id) {
$travel_id= $_POST['travel_id'][$i];
mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
答案 3 :(得分:-2)
foreach($ _POST as $ val){
$province_id = $_POST['province_id'][$i];
$travel_id= $_POST['travel_id'][$i];
mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
我插入1条记录,但我只选择一条,但会将更多数据导入第五条数据。