这是loop.php代码
$compo = $_POST['compo'];
$fill = $_POST['fill'];
$totalCompo = sizeof($compo);
for($i=0;$i<$totalUsername;$i++) {
$InsertUsername = $compo[$i];
$InsertFname = $fill[$i];
$query = "INSERT INTO `Invantsion`(`compon`, `Num`)".
" VALUES ('$InsertUsername','$InsertFname') ";
}
if ($mysqli->query($query) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
在循环页面中我需要插入数据。 这是表格 在mysql中我看到了mysql的最后一个结果
<form method="POST" action="loop.php">
<input type="hidden" name="step" value="save">
<input type='hidden' name='compo[]' value='DECODERS'>DECODERS<select name='fill[]'><option value='NULL'>null</option></select><br><input type='hidden' name='compo[]' value='DECODERS'>DECODERS<select name='fill[]'><option value='100'>100</option></select><br><input type='hidden' name='compo[]' value='rg59'>rg59<select name='fill[]'><option value='300'>300</option><option value='400'>400</option></select><br><input type="submit" value="שלח" name="ok" style="border: 1px solid #000000; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; background-color: #008000; font-family:Arial; font-size:8pt; font-weight:bold"><input type="reset" value="àéôåñ" name="B2" style="border: 1px solid #000000; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; background-color: #008000; font-family:Arial; font-size:8pt; font-weight:bold">
</p>
</form>
我的意思是
<input type='hidden' name='compo[]' value='rg59'>rg59<select name='fill[]'><option value='300'>300</option><option value='400'>400</option>
什么错了?
答案 0 :(得分:0)
您可以利用php中的字符串连接,并使用多个值行构建查询以插入数据。这样,所有数据都将通过单个查询执行来插入。
$_POST['compo'] = array('test1','test2','test3');
$_POST['fill'] = array('fname1','fname2','fname3');
$compo = (!empty($_POST['compo'])?$_POST['compo']:array());
$fill = (!empty($_POST['fill'])?$_POST['fill']:array());
$totalCompo = sizeof($compo);
$query = "INSERT INTO Invantsion(compon, Num) VALUES ";
for($i=0; $i< $totalCompo; $i++)
{
$InsertUsername = (!empty($compo[$i])?$compo[$i]:'');
$InsertFname = (!empty($fill[$i])?$fill[$i]:'');
//Only insert if both username and firstname exists
if(!empty($InsertUsername) && !empty($InsertFname))
{
$query .= " ('".$InsertUsername."','".$InsertFname."'), ";
}
}
//Remove the extra , from the end of the query string
$query = rtrim($query,', ');
if ($mysqli->query($query) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}