php - How to insert multiple rows into database by checked box?

I have a table generated dynamically from a database named 'submittbl'. Each row has a checkbox generated with it dynamically.

I want to be able to insert rows checked into database named 'marktbl'. What I have tried so far only gets the first row saved to database instead of the row I check/select. But if I check all rows, all the rows gets saved properly. How can I insert the checked rows?

I have tried many solutions haven't worked.

//to get records from database dynamically

<?php
            if(isset($_POST['find_btn']))
            {
                if(empty($_POST['s_lev']) && empty($_POST['s_sem']))
                {
                    $msgerr = "<b>Notice: </b>" . "No categories selected.";
                }
                else
                {
                    $selev=$_POST['s_lev'];
                    $sesem=$_POST['s_sem'];
                    $sql = "SELECT *FROM submittbl WHERE CourseCode = ? AND Level = ? AND Semester = ?";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param("sss",$pcode, $plevel, $psems);
                    $pcode = $_SESSION['ccode'];
                    $plevel = $selev;
                    $psems = $sesem;
                    $stmt->execute();
                    $result = $stmt->get_result();
                    $num_rows = $result->num_rows;
                    if($num_rows>0)
                    {
                        while($rows = $result->fetch_assoc())
                        {
                            $sn = $rows['SN'];
                            $name = $rows['StudentName'];
                            $regno = $rows['RegNo'];
                            $asmid = $rows['AssignmentID'];
                            $code = $rows['CourseCode'];
                            $lev = $rows['Level'];
                            $sem = $rows['Semester'];
                            $det = $rows['SubmissionDate'];
                            $doc = $rows['Document'];
                            $score = $rows['Score'];
                            ?>
                            <tr>
                            <td><input type='checkbox' name='delChk[]' value='<?php echo $sn; ?>'><?php echo $sn; ?></input></td>
                            <td><?php echo $name; ?></td>
                            <td><?php echo $regno; ?></td>
                            <td><?php echo $asmid; ?></td>
                            <td><?php echo $det; ?></td>
                            <input type='hidden' name='sn' value='<?php echo $sn; ?>'></input>
                            <input type='hidden' name='name[]' value='<?php echo $name; ?>'></input>
                            <input type='hidden' name='regno[]' value='<?php echo $regno; ?>'></input>
                            <input type='hidden' name='asmid[]' value='<?php echo $asmid; ?>'></input>
                            <input type='hidden' name='ccode[]' value='<?php echo $code; ?>'></input>
                            <input type='hidden' name='level[]' value='<?php echo $lev; ?>'></input>
                            <input type='hidden' name='semester[]' value='<?php echo $sem; ?>'></input>
                            <input type='hidden' name='det[]' value='<?php echo $sub; ?>'></input>
                            <input type='hidden' name='doc[]' value='<?php echo $doc; ?>'></input>
                            <input type='hidden' name='score[]' value='<?php echo $score; ?>'></input>
                            <td><?php echo "<a href='download_submitted.php?id=$asmid'><img src=img/downloads.png width=10% height=10% alt='download'>Download</a>"; ?></td>
                            </tr>
                            <?php
                        }
                    }
                    else
                    {
                        $msgerr = "<b>Notice: </b>" . "Oops! No records found for the selected categories.";
                    }
                }
            }
            ?>

                //to insert into markedtbl database
               //this is where I'm having the whole problem

                <?php
                    if(isset($_POST['upload']))
                    {
                        if(!isset($_POST['delChk']))
                        {
                            $msgerr = "<b>Notice: </b>" . "No records selected - select/mark records to upload.";
                        }
                        else
                        {
                //

Answer

---------get all IDs in checkbox by loop

Answer

-------- $checked = $_POST['delChk']; foreach($checked as $k=>$v) { if($checked[$k] == TRUE) { $chkrow = $checked[$k]; $name = $_POST['name']; $regno = $_POST['regno']; $asmid = $_POST['asmid']; $code = $_POST['ccode']; $level = $_POST['level']; $sems = $_POST['semester']; $det = $_POST['det']; $doc = $_POST['doc']; $score = $_POST['score']; //

Answer

---prepare sql query to update score in database

Answer

Answer

---- $sql = "INSERT INTO marktbl(NSN, StudentName, RegNo, AssignmentID, CourseCode, Level, Semester, DateSubmitted, Document, Score) VALUES(?,?,?,?,?,?,?,?,?,?)"; $stmt = $conn->prepare($sql); $stmt->bind_param("ssssssssss", $pcount, $pname, $pregno, $pasmid, $pcode, $plevel, $psems, $pdet, $pdoc, $pscore); $pcount = $chkrow; $pname = $name[$k]; $pregno = $regno[$k]; $pasmid = $asmid[$k]; $pcode = $code[$k]; $plevel = $level[$k]; $psems = $sems[$k]; $pdet = $det[$k]; $pdoc = $doc[$k]; $pscore = $score[$k]; $stmt->execute(); } } if($conn->error) { $msgerr = $conn->error; } else { echo "<script> alert('Assignment uploaded for marking.')</script>"; } } //} } ?> <?php echo "<span class='err'>" . $msgerr . "</span>"; ?><br> <td class='td'> <button class='upload' name='upload' onclick="return confirm('Are you sure you want to upload selected assignments for marking?')" >Upload for Marking</button> </td> </form>

Answer

Solution:

so I will try to write you a sample of your idea, hope will works. so you have array of users for my example with four keys

$users = [
 0 => [
    'selected' => true,
    'name' => 'Name 1',

  ],
1 => [
    'selected' => false,
    'name' => 'Name 2',

  ],
2 => [
    'selected' => true,
    'name' => 'Name 3',

  ],
3 => [
    'selected' => false,
    'name' => 'Name 4',

  ],
];

foreach ($users as $user) {
    if (!$user['selected]) {
       continue;
    }
    $sql = "INSERT INTO marktbl(
        NSN,
        StudentName,
        RegNo,
        AssignmentID,
        CourseCode,
        Level,
        Semester,
        DateSubmitted,
        Document,
         Score
      ) VALUES(
       :name,
       :lastname,...
      )";
      $stmt = $conn->prepare($sql);
      $params = [
         ':name' => $user['name'],
         ':lastname' => $user['lastname'],...
      ];
      try {
         $stmt->execute($params); 
      } catch(Exception $e) {
         echo $e->getMessage();
      }
      

}

I think your code should look like this. You can easy set a flag for every selected element and then filter data by this flag.

Source