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 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等
  • ¥15 联想交换机NE2580O/NE1064TO安装SONIC
  • ¥15 防火墙的混合模式配置
  • ¥15 Ubuntu不小心注销了要怎么恢复啊