dourang6858 2015-11-16 06:21
浏览 28
已采纳

如何使用codeigniter在mysql中插入多行

I want to insert multiple rows into the db using submit button, This is my view code

    <?php   
        foreach($role_array as $k =>$v)
        {
            //var_dump($k);
            if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" )
            {

        ?>
            <div class="row-fluid"> 
                <div class="span6">      
                    <div class='control-group'>


                        <label for='<?php echo $k;  ?>' class='control-label'><?php  echo $v ;?></label>
                        <div class='controls'>
                            <select class='form-control' name='<?php echo $k;  ?>[]' id='<?php echo $k;  ?>' multiple='multiple' size='5'>
                                <?php

                                   foreach($all_datas[$k] as $datas ) 
                                   {

                                        $username = "{$datas->nickname}";

                                        echo "<option value='{$datas->id}'>{$username}</option>";
                                   }
                                ?>
                            </select>
                            <label for="<?php echo $k;  ?>" class="error" style="display:none"></label>
                        </div>
                    </div>                         
                </div>    

            </div>  
            <?php }} ?> 

This is my controller code

            $roles = $this->utility->roles;
            foreach($roles as $k => $v)
            {
                if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
                {
                    $userrole = $k;
                    $post_user=isset($post_data[$k])?$post_data[$k]:'';

                    if (count($post_user) > 0)
                    {
                        for ($i = 0; $i < count($post_user); $i++)
                        {
                            $data = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
                           // var_dump($data);
                            $flag = true;



                        }
                    }
                }
            }

            if(isset($flag))
            {

                $v=$this->JournalUsers->addJournalUser($data);
            }

$data output is

    array(3) {
    ["journal_id"]=>int(83)
    ["user_role"]=>
    string(15) "quality_checker"
    ["user_id"]=>
    string(3) "136"
    }
    array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "103"
    }
    array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "133"
    }
     array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "132"
    }
    array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>
   string(10) "typesetter"
   ["user_id"]=>
   string(3) "109"
   }

   array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>
   string(10) "typesetter"
   ["user_id"]=>
   string(3) "153"
   }

But,when insert the data only one row will be inserted on the table.

   array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>`enter code here`
    string(10) "typesetter"
    ["user_id"]=>
    string(3) "153"
    }  

I want to insert all datas in db

  • 写回答

2条回答 默认 最新

  • douqiang6448 2015-11-16 06:30
    关注

    Here the $data is reinitialized everytime. Update the code like this:

    $data = array();
        $roles = $this->utility->roles;
            foreach($roles as $k => $v)
            {
                if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
                {
                    $userrole = $k;
                    $post_user=isset($post_data[$k])?$post_data[$k]:'';
    
                    if (count($post_user) > 0)
                    {
                        for ($i = 0; $i < count($post_user); $i++)
                        {
                            $data[] = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
                           // var_dump($data);
                            $flag = true;
    
    
    
                        }
                    }
                }
            }
    
            if(isset($flag) && !empty($data))
            {
    
                $v=$this->JournalUsers->addJournalUser($data);
            }
    

    And in the model loop through like this:

    public function addJournalUser($data) {
        foreach ($data AS $row) {
          // execute your insertion query with variable row   
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为啥快手广告联盟的广告这么难出来
  • ¥15 k8s集群重启后,kubelet一直报systemctl restart kubelet.service "Failed to delete cgroup paths"
  • ¥15 微信小程序构建工具类发布到npm,install后报错
  • ¥15 amesim液压仿真问题
  • ¥15 MATLAB优化设计
  • ¥15 ANN预测模型 200大米
  • ¥20 优秀学生的问题 oj java
  • ¥15 scratch-gui 如何集成进 react native
  • ¥15 把全局悬浮窗改为局内悬浮窗
  • ¥15 Excel vba编程调用word如何读取换行符