1

When being saved to database, it iterates like this The foreach ($ac) keeps on iterating based on the clicked checkboxes, can somebody help me please this is for my project. I think there is sonething wrong with my logic on the foreach but i dont know where it is exacty. Please debug this anyone. :))))))))))))))))))))))))))))))

 <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>

1 Answers1

1

Your problem was caused by your nested foreach loops. Because quantity and ac_description are two arrays, nesting the loops gives you every combination, which means the same item occurs multiple times.

eg: if $a = [1,2] and $b = [3,4], nesting loops will give you

foreach($a as $first){
  foreach($b as $second){
    echo "$first, $second"; // 1,3  1,4  2,3   2,4
  }
}

What you should do instead is have one loop that picks up the corresponding term from both arrays

for($i=0, $limit=count($a); $i < $limit; $i++){
  echo $a[$i] . ', ' . $b[$i]; // 1,3   2,4
}

So to get back to your problem, you can get around it with something like:

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
}

Note 1: Although this will work, note that you should always avoid doing DB queries in a loop because they will slow down your script a lot. Instead, learn how to do multiple inserts in one query.

Note 2: Your code is very vulnerable to SQL injection attacks which means it would be easy for someone to modify, steal, delete etc... your database. Learn about prepared statements.

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • that code is giving me an error Notice: Undefined offset: 11, the array values which is the $i is undefined – Joana Clariz Jacinto Aug 28 '17 at 07:28
  • @JoanaClarizJacinto This error means that your description array is shorter than your quantity array because `$i` is based on the size of `$quantity`. This will happen if in the HTML, there is some input for one quantity field, but the matching description checkbox is not checked. How to handle that situation depends on your application requirements so I can't tell you what to do about it.. – BeetleJuice Aug 28 '17 at 10:28