使用codeigniter,动态输入值无法正确保存在数据库中

时间:2017-08-03 03:07:29

标签: php jquery mysql sql codeigniter

开发以下代码以使用codeigniter框架在数据库中保存4个动态输入数据。但由于错误,第一个输入框的值仅保存了4次。我已经包含了代码和屏幕截图。有人可以帮我纠正这个问题吗? 这是我向数据库输入值的方式 enter image description here

enter image description here 但这是它在数据库中保存的方式

这是我的控制器

function error(){
    //if ($this->input->post('mytext')) {

       $attain = $this->input->post('mytext', true);
        $data2=array(); //<-initialize
            foreach ($attain as $i => $a) { // need index to match other properties
                //append array
                $data2[] = array(
                'mytext' => $a,
                'mytext1' => $a,
                'mytext2' => $a,
                'mytext3' => $a,
                'projectname'=> $this->input->post('projectname'),
            );
            //for multiple entry in same table


        //}
    }
    $this->db->insert_batch('projectem', $data2); 
    redirect('Select_ctrl2/ModalAddEmployeesProject');
  } 

这是我的观点

 <link rel = "stylesheet" type = "text/css" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/css/jquery.multiselect.css">

<link rel = "stylesheet" type = "text/javascript" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery-1.5.min.js">

 <link rel = "stylesheet" type = "text/javascript" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery.multiselect.js">



<head>
<script>
</script>
<script type="text/javascript">
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('</br><div class="col-lg-12"><div class="col-lg-3"><input class="input form-control" placeholder="Task Name" name="mytext[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Description" name="mytext1[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Task Cost" name="mytext2[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Employee" name="mytext3[]"/></div><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script> 
</head>


    <body>

<div class="col-lg-10">
    <div class="panel panel-danger">
        <div id="page-wrapper " style="background-color: #f8f8f8">

            <div class="container-fluid shadow " >


                <div class="row" style="background-color:#46bc99;">
                    <div class="col-lg-12" >
                        <h3 class="text-center formheading">
                           <li class="glyphicon glyphicon-user" style="font-size: 40px; padding-top: 2px;"></li>
                           ADD EMPLOYEES TO PROJECT
                        </h3>

                    </div>

                </div>

                <br>
                <div class="panel-body" align="center" style="width: 960px; color: navy;  border: 2px black; padding: 5px;">

            <div class="col-lg-12" >

                <?php echo form_open('Select_ctrl2/error'); ?>



                <div class="form-group">
                <select name="projectname" class="input form-control">
                    <option value="none" selected="selected">Select Project</option>


                    <?php foreach($projects as $s):?> 
                    <option value="<?php echo $s->projectname?>"><?php echo $s->projectname?></option>
                    <?php endforeach;?>  
                </select>
                </div>

                <div class="input_fields_wrap">
                    <div class="form-group">
                    <button type="button" class="btn btn-success add_field_button">Add More Fields</button> 
                    </div>

                </div>
                <div>

                 <button name="submit" type="submit" class="btn btn-info">Submit</button>
                </div>

                <?php echo form_close(); ?>


            </div>





            </div>             

</div>


    </body>
</html>

1 个答案:

答案 0 :(得分:1)

您正在覆盖该foreach上的每次迭代中的值,请记住您将mytext,mytext1,mytext2和mytext3作为数组接收,因此您需要通过其索引访问每个值:

function error(){
       $attain = $this->input->post();
       $data2=array(); //<-initialize
       for ($i = 0; $i < count($attain['mytext']); $i++) {
                //append array
                $data2[] = array(
                    'mytext' => $attain['mytext'][$i],
                    'mytext1' => $attain['mytext1'][$i],
                    'mytext2' => $attain['mytext2'][$i],
                    'mytext3' => $attain['mytext3'][$i],
                    'projectname'=> $attain['projectname'],
                );
                //for multiple entry in same table
       }
    $this->db->insert_batch('projectem', $data2); 
    redirect('Select_ctrl2/ModalAddEmployeesProject');
  }