简体   繁体   中英

how to get all rows in column to insert db in codeigniter

I'm trying to insert all rows which showing view in a table but it insert only one row with value '1' not all rows and not that id's contain in a column:

view:

echo"<table class='table table-hover type-list2'id='traineeList'>
                    <tr class='success'>
                        <th>Trainee ID</th>
                        <th>Trainee Name</th>
                        <th>Present</th>
                    </tr>";
            foreach($list['trainee'] as $row){ 
                echo "
                    <tr><td>".str_pad($row->TraineeID,7,"0", STR_PAD_LEFT)."</td>
                        <td>".$row->Name."</td>
                        <td><input type='checkbox' name='.$row->TraineeID[]' value='".$row->TraineeID."' checked></td>
                    </tr>";
            }
            echo "</table>";

controller:

public function insertAttendance(){
    $data = array('TraineeID'>=$this->input->post('TraineeID'));
    $attnDate=$this->input->post('attnDate');
    $classHour= $this->input->post('classHour');
    foreach ($data as $id){
        $query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
        $this->db->query($query);
        redirect('attendance/index/');

    }

Your redirect function should be placed outside of your foreach loop, probably. Looking at your code, your foreach loop will run only once and then redirect you to another page.

Please try with this

    foreach($_POST['TraineeID'] as $key => $id) {

        $attnDate=$_POST['attnDate'][$key];
        $classHour= $_POST['classHour'][$key];

        $query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
        $this->db->query($query);
    }
     redirect('attendance/index/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM