简体   繁体   English

如何获取列中的所有行以在Codeigniter中插入db

[英]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: 我试图将所有显示视图的行插入表中,但它仅插入一个值为'1'的行,而不是所有行,并且一列中不包含该ID:

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. 您的重定向功能可能应该放在foreach循环之外。 Looking at your code, your foreach loop will run only once and then redirect you to another page. 查看您的代码,您的foreach循环将只运行一次,然后将您重定向到另一页。

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/');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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