mercredi 13 juin 2018

Foreach not showing checkbox values for the correct rows after submit

i'm trying to get the Foreach display the correct checkbox values after submission however the checkbox values does not seem to tally with the correct rows that was checked. Below is my code for the foreach loop:

<?php
include('mysql_connect.php');


$stmtselect = $conn->prepare("SELECT c.ID, a.SN, a.NAME, a.NRIC, c.REG_NO, c.OT, c.PT, c.ST, c.MSW FROM patient_master a, patient_referral b, patient_screening c WHERE a.sn = b.patient_id and b.reg_no = c.reg_no");
mysqli_stmt_execute($stmtselect);
$result = mysqli_stmt_get_result($stmtselect);

if(isset($_POST['submit'])){

        $ot = $_POST['ot'];
        $rowid = $_POST['row_id'];
        $pt = $_POST['pt'];
        $st = $_POST['st'];
        $msw = $_POST['msw'];

    foreach($ot as $i => $ot1) {

        $rowid1 = $rowid[$i];
        $pt1 = $pt[$i];
        $st1 = $st[$i];
        $msw1 = $msw[$i];

       echo "$rowid1, $ot1, $pt1, $st1, $msw1 <br/>";
    }

}
?>

And below is the html form:

<form method="post" id="create_screening" action="" name="frm" enctype="multipart/form-data">

<input type="submit" name="submit" class="btn btn-success" value="Save Therapy Referral">
            <table>
              <thead>
                <tr>
                                        <th>Name</th>               
                                        <th>Registration No.</th>
                                        <th>OT</th>
                                        <th>PT</th>
                                        <th>ST</th>
                                        <th>MSW</th>
                </tr>
              </thead>

                <tbody>
                <?php 
                                    while($fetch = $result->fetch_assoc()){

                                           $screening_id = $fetch['ID'];
                                           $id = $fetch['SN'];
                                           $name = $fetch['NAME'];
                                           $nric = $fetch['NRIC'];
                                           $reg_no= $fetch['REG_NO'];
                                           $ot = $fetch['OT'];
                                           $pt = $fetch['PT'];
                                           $st = $fetch['ST'];
                                           $msw = $fetch['MSW'];


                                echo"
                                    <tr>
                                        <td>$name</td>
                                        <td><b>$reg_no<b/></td>";                                                               
                                ?>

                                    <input type="hidden" id="row_id" name="row_id[]" value="<?php echo $reg_no; ?>">
                                <td><input type="checkbox" id="ot" name="ot[]" value="Y" class="form-control" <?php if($ot=="Y"){ echo "checked"; }?>></td>
                                <td><input type="checkbox" id="pt" name="pt[]" value="Y" class="form-control" <?php if($pt=="Y"){ echo "checked"; }?>></td>
                                <td><input type="checkbox" id="st" name="st[]" value="Y" class="form-control" <?php if($st=="Y"){ echo "checked"; }?>></td>
                                <td><input type="checkbox" id="msw" name="msw[]" value="Y" class="form-control" <?php if($msw=="Y"){ echo "checked"; }?>></td>

                                </tr>       
                                <?php 
                                    }
                                    ?>
                </tbody>              


    </table>
</form>

Below is the screenshot before and after submission.

Before submission:

enter image description here

Output after submission showing the check values for the incorrect registration numbers:

enter image description here

Can anyone help find out what's wrong with my code. Thanks




Aucun commentaire:

Enregistrer un commentaire