When being saved to database, it iterates like this foreach($ ac)根据点击的复选框继续迭代,有人可以帮助我,这是我的项目。我认为我对foreach的逻辑有些错误,但我不知道它的确切位置。请调试这个人。 :))))))))))))))))))))))))))))))
<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>
答案 0 :(得分:1)
您的问题是由嵌套的foreach
循环引起的。因为quantity
和ac_description
是两个数组,所以嵌套循环会为您提供每个组合,这意味着同一个项目会多次出现。
例如:if $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:虽然这会有效,但请注意,您应该始终避免在循环中进行数据库查询,因为它们会大大减慢脚本速度。相反,请学习如何做multiple inserts in one query。
注2:您的代码非常容易受到SQL注入攻击,这意味着某人可以轻松修改,窃取,删除等数据库。了解prepared statements。