When being saved to database, it iterates like this
foreach($ac)继续基于单击的复选框进行迭代,有人能帮我一下吗?这是我的项目。我认为我的逻辑有点不对劲,但我不知道哪里是正确的。请调试任何人。:))))))))))))))))))))))))))))))
<html>
<body>
<div class="image">
<img src="PLM1.png" alt="plmbackground" height="650" width="1351"/>
</div><br><br><br><br>
<form method = "post" class="content">
<font size= '5px'>Student ID <input type="text" name = "student_id"> <br>
OR NO <input type = "text" name = "or_no"> <br>
</font>
<table align="center">
<tr valign="middle" align="center">
<td><font color="red"><b>REQUESTS</b></font>
<td><font color="red"><b>QUANTITY</b></font>
</tr>
<tr>
<td><input type = "checkbox"name = "ac_description[]" value = "Replacement_of_Registration"><b>Replacement of Registration</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox"name = "ac_description[]" value = "Good Moral Certificate"><b>Good Moral Certificate</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "Honorable Dismissal "><b>Honorable Dismissal</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "Transcript of Record"><b>Transcript of Record</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "Diploma"><b>Diploma</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "CUE Request"><b>CUE Request</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "CMI Request"><b>CMI Request</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "Entrance Exam"><b>Entrance Exam</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "School fees-Medical/Dental Laboratory "><b>School fees-Medical/Dental Laboratory</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "School fees-Transcript/Honorable"><b>School fees-Transcript/Honorable</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "School fees-Library"><b>School fees-Library</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
<tr>
<td><input type = "checkbox" name = "ac_description[]" value = "Affiliation Fees"><b>Affiliation Fees</b>
<td><center><input type="number" name="quantity[]" style="width:60px;"></center>
</tr>
</table>
<br>
<input type = "submit" name = "send" value = "Add" class="btn-5" >
</form>
<?php
//the database connection
$db = mysqli_connect ("localhost", "root", "turtledove", "accounting");
if (!$db)
{
die ("ERROR!!!!!!>>>");
}
$student_id = $_POST["student_id"];
$or_no = $_POST["or_no"];
$status1="processing";
$qty=1;
$col_credit = 80;
$dep_credit = 80;
$col_debit = 0;
$dep_debit = 0;
$quantity = $_POST["quantity"];
$ac_description = $_POST["ac_description"];
if (($quantity)&&($ac_description) )
{
foreach ($quantity as $quantity)
{
foreach ($ac_description as $ac)
{
mysqli_query($db, "insert into or_header (or_no, ac_description, student_id, date1, status1, qty,
col_credit, col_debit, dep_credit, dep_debit)
values (".$or_no.",'".mysqli_real_escape_string($db,$ac)."',
'".$student_id."',curdate(),'processing',".$quantity.",80,0,80,0)");
}
}
}
?>
<form action="cashiermainpage.php">
<input type="submit" method="POST" value="Mainpage" class="mainpage" alt="Submit">
</form>
</body>
</html>
最佳答案
您的问题是由嵌套的foreach
循环引起的。因为quantity
和ac_description
是两个数组,所以嵌套循环会给出每个组合,这意味着同一项会多次出现。
例:如果$a = [1,2]
和$b = [3,4]
,嵌套循环将为您提供
foreach($a as $first){
foreach($b as $second){
echo "$first, $second"; // 1,3 1,4 2,3 2,4
}
}
相反,您应该做的是使用一个循环从两个数组中提取相应的项
for($i=0, $limit=count($a); $i < $limit; $i++){
echo $a[$i] . ', ' . $b[$i]; // 1,3 2,4
}
所以要回到你的问题上来,你可以用以下方法来解决:
for($i=0, $limit=count($quantity); $i < $limit; $i++){
$qty = $quantity[$i];
$ac = $ac_description[$i];
// now you can run your query with the $qty and its matching $ac
}
注意1:虽然这是可行的,但是请注意,您应该始终避免在循环中执行DB查询,因为它们会大大降低脚本的速度。相反,学习如何做multiple inserts in one query。
注意2:您的代码很容易受到SQL注入攻击,这意味着有人很容易修改、窃取、删除等。。。你的数据库。了解prepared statements。
关于php - foreach会根据单击的复选框数继续进行迭代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45847467/