使用codeigniter在mysql中插入多行:它仅插入一行,并且在该行中仅存储一个第一个字符

时间:2018-09-19 04:46:52

标签: php mysql codeigniter

我试图在sql中插入多行。但是它仅插入最后一行,而在那一行仅存储每一列的第一个字符。我通过回显打印查询,它仅显示最后一行,但给出每一列的所有字符。另一件事是,iam通过单击提交按钮在两个表中插入值。有人可以帮忙吗?

这里是视图:

    <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

<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("/\r|\n/", "", form_dropdown('qualification', $education,'' ,'required="" class="form-control"'))); ?>';
        var jobtype = '<?php echo str_replace("'", '', preg_replace("/\r|\n/", "", 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>

这里是控制器:

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

                }

型号:

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);
            }
        }

第一个表给出的结果正确,但是 更多字段的输出如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

尝试在数组下面尝试,然后从insCompanies函数中删除$ cp_data [] = array(....)。

$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]
                );