php - Multiple insert array POST values from foreach loop into mysql -


<table class="table table-bordered">     <thead>         <tr>             <th class="col-md-2">first name</th>             <th class="col-md-2">last name</th>             <th  class="col-md-2">present</th>             <th class="col-md-2">absent</th>         </tr>     </thead>      <?php foreach($teacher_names $name): ?>          <tbody>             <tr>                 <td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="f_name" value="<?php echo $name['first_name']; ?>"></td>                 <td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="l_name" value="<?php echo $name['last_name']; ?>"></td>                 <td class="col-md-2"><input type="checkbox" name="present" value="y"></td>                 <td class="col-md-2"><input type="checkbox" name="absent" value="n"></td>              </tr>         </tbody>     <?php endforeach;?> </table> 

i want able access values in database @ moment 10. want $_post values , insert them database seems 1 post value in foreach loop last key value. example of php script

public function teachers_attendance($first_name, $last_name, $present, $absent){                         $sql = "insert teacher_attendance($first_name, $last_name, $present, $absent)              values(:first_name, :last_name, :present, :absent)";      $stmt = $this->_connection->prepare($sql);      $stmt->bindvalue(':first_name', $first_name, pdo::param_int);     $stmt->bindvalue(':last_name', $last_name, pdo::param_str);     $stmt->bindvalue(':present', $present, pdo::param_str);     $stmt->bindvalue(':absent', $absent, pdo::param_str);     $stmt->execute();       $num_rows = $stmt->rowcount();      if($num_rows > 0){         echo "yes";     }else{         echo "no";     }  } 

i want able access post value , checkbox values array , insert them in database. grateful if can have solutions problem.

since didn't click me first time un-matching keys when checkboxes not selected, i've written method of accomplishing you're after.

so template should follows:

<table class="table table-bordered">     <thead>         <tr>             <th class="col-md-2">first name</th>             <th class="col-md-2">last name</th>             <th  class="col-md-2">present</th>             <th class="col-md-2">absent</th>         </tr>     </thead>     <tbody>     <?php      $i = 0;     foreach($teacher_names $name):         ?>         <tr>             <td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="f_name_<?php echo $i; ?>" value="<?php echo $name['first_name']; ?>" /></td>             <td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="l_name_<?php echo $i; ?>" value="<?php echo $name['last_name']; ?>" /></td>             <td class="col-md-2"><input type="checkbox" name="present_<?php echo $i; ?>" value="y" /></td>             <td class="col-md-2"><input type="checkbox" name="absent_<?php echo $i; ?>" value="n" /></td>         </tr>         <?php         $i++;     endforeach;     ?>     </tbody> </table> 

then start @ 0, add 1 each time, , loop until numbered name fields no longer found (at point assumed there no more entries)

<?php  // first row check 0, per template $i = 0;  // loop while f_name_* , l_name_* found (if not found, assumed there no more rows) while( isset( $_post['f_name_'.$i] ) && isset( $_post['l_name_'.$i] ) ) {     // , set name variables (based on current row)     $first_name = $_post['f_name_'.$i];     $last_name  = $_post['l_name_'.$i];      // presence , absence checkboxes y value, we'll set these 'n' if not found     $present    = ( isset( $_post['present_'.$i] ) ? $_post['present_'.$i] : 'n' );     $absent     = ( isset( $_post['absent_'.$i] )  ? $_post['absent_'.$i]  : 'n' );      // call teachers_attendance function     teachers_attendance($first_name, $last_name, $present, $absent);      // increment row check     $i++; }  ?> 

Comments