dongxin8709 2018-09-19 04:46
浏览 68
已采纳

使用codeigniter在mysql中插入多行:它只插入一行,而一行只存储一个第一个字符

I tried to insert multiple rows in sql . But it inserts only the last one row and in that one row only storing first character of each column. I prints the query by echo, it shows only one last row, but gives all characters of each column. one more thing is iam inserting values in two tables by clicking on submit button. can anyone help on this.

Here it is the view :

    <form class="" method="POST" enctype="multipart/form-data" action="<?php echo base_url(); ?>dashboard/addnewjobmela" >

                        <input type="hidden" name="csrfmiddlewaretoken" value="LgVIVf7yFe5bL9k2Rcj9TGLLpgKJX1LkmfiiptEZnN95y9WqKXHk7V4vGixmo6Wd">
                        <input type="hidden" id="cperson_no" value="1">
                        <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Job Mela Title</label>
                            <input class="form-control" type="text" name="title"  required="" id="title">
                        </div>
                        </div>
                       <div class="col-xs-12 col-sm-12 col-md-4">
                        <div class="form-group">
                            <label>Job Mela Date</label>
                            <input class="form-control" type="date" name="date"  required="" id="date">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4">
                        <div class="form-group">
                            <label>Last Date To Register</label>
                            <input class="form-control" type="date" name="laastdatetoregister"  required="" id="laastdatetoregister">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 ">
                        <div class="form-group">
                            <label>Venue Details</label>
                            <textarea class="form-control" name="venuedetails" cols="40" rows="2" required="" id="venuedetails"></textarea>
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Contact Person</label>
                            <input class="form-control" type="text" name="contactperson"  required="" id="contactperson">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Contact Emailid</label>
                            <input class="form-control" type="text" name="emailid"  required="" id="emailid">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Contact Number</label>
                            <input class="form-control" type="text" name="contactnumber"  required="" id="contactnumber">
                        </div>
                        </div>
                        </div>
                        <div class="row-fluid" >
                        <div style="background-color:#d6e9c6 !important;padding:10px"><p class="text-success"><b>Participating Companies</b></p></div>
                        </div>
                        <div class="col-xs-12 right" style="margin-top:-40px;margin-bottom: 20px">
                        <button class="btn btn-success" type="button" onclick="addingcompanies()">Add More</button>
                        </div>
                        <div id="companies">
                        <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Company Name</label>
                            <input class="form-control" type="text" name="company"  required="" id="company">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Job Title</label>
                            <input class="form-control" type="text" name="jobtitle"  required="" id="jobtitle">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Required Qualification</label>
                            <?php
                                echo form_dropdown('qualification', $education,'' ,'required="" class="form-control"');
                            ?>
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Specialization</label>
                            <input class="form-control" type="text" name="specialization"  required="" id="specialization">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Sector</label>
                            <input class="form-control" type="text" name="sector"  required="" id="sector">
                        </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-4">
                        <div class="form-group">
                            <label>Job Type</label>
                            <?php
                                echo form_dropdown('jobtype', $jobtype,'' ,'required="" class="form-control"');
                            ?>
                        </div>
                        </div>
                        <!-- <div class="col-xs-12 col-sm-12 col-md-4">
                        <div class="form-group">
                            <label>Company Logo</label>
                            <input class="form-control" type="file" name="picture" accept=".jpeg,.JPEG,.JPG,.jpg,.png,.PNG" required="" id="id_picture">
                        </div>
                        </div> -->
                        <div class="col-xs-12 col-sm-12 col-md-4 ">
                        <div class="form-group">
                            <label>Job Location</label>
                            <input class="form-control" type="text" name="joblocation"  required="" id="joblocation">
                        </div>
                        </div>
                        </div>
                        </div>

                        <div class="col-xs-12" style="text-align:center">
                            <button class="btn btn-success" type="submit" value="Save Profile">Submit</button>
                            </div>
                    </form>

Javascript/Jquery to add ADD More Fields

<script type="text/javascript">

function addingcompanies()
    {
      var cperson_no = $('#cperson_no').val();
                          var j=parseInt(cperson_no)+1;
                          $('#cperson_no').val(j);
        var qualification = '<?php echo str_replace("'", '', preg_replace("/|
/", "", form_dropdown('qualification', $education,'' ,'required="" class="form-control"'))); ?>';
        var jobtype = '<?php echo str_replace("'", '', preg_replace("/|
/", "", form_dropdown('jobtype', $jobtype,'' ,'required="" class="form-control"'))); ?>';
        $('#companies').append('<div class="row group"><hr/><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Company Name</label> <input class="form-control" type="text" name="company" required="" id="company' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Job Title</label> <input class="form-control" type="text" name="jobtitle" required="" id="jobtitle' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Required Qualification</label> '+ qualification +'</div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Specialization</label> <input class="form-control" type="text" name="specialization" required="" id="specialization' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Sector</label> <input class="form-control" type="text" name="sector" required="" id="sector' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4"> <div class="form-group"> <label>Job Type</label>'+jobtype+' </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Job Location</label> <input class="form-control" type="text" name="joblocation" required="" id="joblocation' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-4 col-md-4 "><button class="btn btn-danger" style="margin-top: 25px; !important;" type="button" onClick="con_grpremove(this)">Remove</button></div></div></div>');


    }
    function con_grpremove(obj) {
                                $(obj).closest(".group").remove();
                            }
</script>

Here it is the controller :

public function addnewjob()
        {

            $this->load->model('dashboard_model');
            $result = $this->dashboard_model->addnewjob();
            $this->session->msg = "New Job Added Successfully";
            redirect('dashboard');

        }
                public function addnewjobmela()
                {

                        $this->load->model('dashboard_model');
                        $result = $this->dashboard_model->addnewjobmela();
                        $this->session->msg = "New Job Mela Added Successfully";
                        redirect('dashboard');

                }

Model :

public function addnewjobmela() {
        $data['title'] = $this->input->post('title');
        $data['date'] = $this->input->post('date');
        $data['lastdate'] = $this->input->post('laastdatetoregister');
        $data['venue'] = $this->input->post('venuedetails');
        $data['contactperson'] = $this->input->post('contactperson');
        $data['emailid'] = $this->input->post('emailid');
        $data['contactnumber'] = $this->input->post('contactnumber');
        $data['status'] = 1;
        $data['id'] = $this->db->insert_id();
        print_r($data);
        $this->db->insert('jobmelas', $data);
        $jobmelaid = $this->db->insert_id();
        $jcdata['company'] = $this->input->post('company');
        $jcdata['jobtitle'] = $this->input->post('jobtitle');
        $jcdata['qualification'] = $this->input->post('qualification');
        $jcdata['specialization'] = $this->input->post('specialization');
        $jcdata['sector'] = $this->input->post('sector');
        $jcdata['jobtype'] = $this->input->post('jobtype');
        $this->insCompanies($jobmelaid, $jcdata);
    }
    public function insCompanies($jobmelaid, $jcdata, $update='') {
            if($update == 'update'){
                $this->db->query('delete from job_mela_companies where jobmelaid = '.$jobmelaid.'');
            }
            print_r($jcdata);
            for ($i = 0; $i < count($jcdata['company']); $i++) {
                $cp_data[] = array(
                    'jobmelaid' => $jobmelaid,
                    'company' => $jcdata['company'][$i],
                    'jobtitle' => $jcdata['jobtitle'][$i],
                    'qualification' => $jcdata['qualification'][$i],
                    'specialization' => $jcdata['specialization'][$i],
                    'jobtype' => $jcdata['jobtype'][$i],
                    'sector' => $jcdata['sector'][$i]
                );

                $this->db->insert('job_mela_companies', $cp_data);
            }
        }

First table is giving result correct but The output of more fields giving like this: enter image description here

  • 写回答

1条回答 默认 最新

  • dtwncxs3547 2018-09-19 05:43
    关注

    try below array and remove $cp_data[] = array(....) from insCompanies function.

    $cp_data = array(
                    'jobmelaid' => $jobmelaid,
                    'company' => $jcdata['company'][$i],
                    'jobtitle' => $jcdata['jobtitle'][$i],
                    'qualification' => $jcdata['qualification'][$i],
                    'specialization' => $jcdata['specialization'][$i],
                    'jobtype' => $jcdata['jobtype'][$i],
                    'sector' => $jcdata['sector'][$i]
                    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 3DTiles数据转换
  • ¥15 c++代码数据结构理解含义
  • ¥15 anaconda相关启动程序问题
  • ¥15 关于#hyperledger#的问题,如何解决?
  • ¥30 使用opencv中的solvePnPRansac计算相机位姿后,误差较大
  • ¥15 串口控制LED灯驱动板设计
  • ¥15 如何在unity场景里做到通过按钮实现模型替换
  • ¥15 uniapp的websocket和c++的websocket服务器通讯 发送的数据和接受的数据表现形式不一样
  • ¥15 pyspark配置中的错误
  • ¥15 html word表格