我是Codeigniter的新手可以任何人帮我如何将数据插入mysql?

时间:2016-07-14 11:20:02

标签: php mysql codeigniter

我正在学习Codeigniter我想将数据插入数据库。我该如何执行此类任务? 这是我的控制器

<?php
  defined('BASEPATH') OR exit('No direct script access allowed');

 class PostJobs extends CI_Controller {
public function __construct() {
    parent::__construct();   
    $this->load->model('Post_Model');     
}

public function index(){        
    $this->load->helper(array('form', 'url', 'email'));
    $this->load->view('postjobs');          
}
public function post(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email',
        array(
            'required' => 'enter email id'));

    $this->form_validation->set_rules('JTitle', 'Job Title', 'trim|required|callback__check_length[6]',
        array(
            'required' => 'enter JOB TITLE'));

    $this->form_validation->set_rules('JType', 'Job Type', 'trim|required',
        array(
            'required' => 'select job type'));

    $this->form_validation->set_rules('Salary', 'Salary', 'trim|required',
        array(
            'required' => 'enter salary'));

    $this->form_validation->set_rules('Exp', 'Experience', 'trim|required',
        array(
            'required' => 'select Experience'));

    $this->form_validation->set_rules('Skills', 'Skills', 'trim|required',
        array(
            'required' => 'enter skills'));

    $this->form_validation->set_rules('EduReq', 'Education required', 'trim|required',
        array(
            'required' => 'enter EDUCATION'));

    $this->form_validation->set_rules('JLoc', 'Location', 'trim|required',
        array(
            'required' => 'enter LOCATION'));

    $this->form_validation->set_rules('CName', 'Company Name', 'trim|required',
        array(
            'required' => 'enter COMPANY NAME'));

    $this->form_validation->set_rules('CAdd', 'Company Address', 'trim|required',
        array(
            'required' => 'enter COMPANY ADDRESS'));

    $this->form_validation->set_rules('JDesc', 'Job Description', 'trim|required',
        array(
            'required' => 'enter JOB DESCRIPTION'));

    if($this->form_validation->run() ==FALSE){
        $this->load->view('postjobs');  
    }
    else{           
        $data = array(
            'email' => $this->input->post('email'),
            'JTitle' => $this->input->post('JTitle'),
            'JType' => $this->input->post('JType'),
            'Salary' => $this->input->post('Salary'),
            'Exp' => $this->input->post('Exp'),
            'Skills' => $this->input->post('Skills'),
            'EduReq' => $this->input->post('EduReq'), 
            'JLoc' => $this->input->post('JLoc'),
            'CName' => $this->input->post('CName'),
            'CWeb' => $this->input->post('CWeb'),
            'CAdd' => $this->input->post('CAdd'),
            'JDesc' => $this->input->post('JDesc')
        );
        $this->Post_Model->insert_postjob($data);
        $this->load->view('postjobs');
    }
}
function _check_length($input, $min){
    $length = strlen($input);

    if ($length >= $min){
        return TRUE;
    }
    elseif ($length < $min){
        $this->form_validation->set_message('_check_length', 'Minimum ' . $min. ' character required');
        return FALSE;        
    }    
}   
}

这是我的模特:

<?php
   defined('BASEPATH') OR exit('No direct script access allowed');

   class Post_Model extends CI_Model {
public function __construct() {
    parent::__construct();        
}

public function insert_postjob($data){      
    $this->db->insert('jobs', $data);
}
}

作业是表名

我的观点

<div class="postjob">
           <div class="container">
              <h3>Post a Job</h3>
              <form class="form-horizontal" role="form" id="postjobs" role="form" method="POST" action="<?=site_url('PostJobs/post');?>">                     
                 <div class="form-group">
                    <div class="col-lg-12">
                       <label for="jobemail">Email</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block; text-transform:uppercase;"><?php echo form_error('email');?></span>
                       <input type="email" id="emailjob" name="email" class="input-job form-control" value="<?php echo set_value('email'); ?>" autofocus>
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-6">
                       <label for="jobtitle">Job Title</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JTitle');?></span>
                       <input type="text" id="titlejob" name="JTitle" class="input-job form-control"  value="<?php echo set_value('JTitle'); ?>">
                    </div>
                    <div class="col-lg-6">
                       <label for="jobtype">Job Type</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JType');?></span>
                       <select name="JType" class="input-job form-control" id="select" >
                          <option value="">SELECT</option>
                          <option value="Full Time">Full Time</option>
                          <option value="Part Time">Part Time</option>
                          <option value="Temporary">Temporary</option>
                          <option value="Internship">Internship</option>
                       </select>                          
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-6">
                       <label for="jobsalary">Salary</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Salary');?></span>
                       <input type="text" name="Salary" id="salaryjob" class="input-job form-control"  value="<?php echo set_value('Salary');?>">                           
                    </div>
                    <div class="col-lg-6">
                       <label for="exp">Experience</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Exp');?></span>
                       <select name="Exp" class="input-job form-control" id="exp" >
                          <option value="">SELECT</option>
                          <option value="Fresher">Fresher</option>
                          <option value="< 1"> &lt; 1</option>
                          <option value="1-2">1-2</option>
                          <option value="2-5">2-5</option>
                          <option value="5-8">5-8</option>
                          <option value="8-10">8-10</option>
                          <option value="10-12">10-12</option>
                          <option value="> 12">&gt; 12</option>
                       </select>                           
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-6">
                       <label for="skills">Skills(Seperate by Comma)</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Skills');?></span>
                       <input type="text" name="Skills" id="skill" class="input-job form-control" value="<?php echo set_value('Skills');?>">
                    </div>
                    <div class="col-lg-6">
                       <label for="education">Education Requirement</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('EduReq');?></span>
                       <input type="text" name="EduReq" id="EduReq" class="input-job form-control" value="<?php echo set_value('EduReq');?>">                           
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-6">
                       <label for="joblocation">Job Location</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JLocation');?></span>
                       <input type="text" name="JLocation" id="locationjob" value="<?php echo set_value('JLocation');?>" class="input-job form-control">                           
                    </div>
                    <div class="col-lg-6">
                       <label for="cname">Company Name</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CName');?></span>
                       <input type="text" name="CName" id="cname" value="<?php echo set_value('CName');?>" class="input-job form-control">  
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-6">
                       <label for="cweb">Company Website(Optional)</label>
                       <input type="url" name="CWeb" id="cweb" value="<?php echo set_value('CWeb');?>" class="input-job form-control">
                    </div>
                    <div class="col-lg-6">
                       <label for="cadd">Company Address</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CAdd');?></span>
                       <input type="text" class="input-job form-control" name="CAdd" value="<?php echo set_value('CAdd');?>">
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-12">
                       <label for="jobdesc">Job Description</label>
                       &nbsp;&nbsp;<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JDesc');?></span>
                       <textarea class="input-job form-control textarea" name="JDesc" rows="10" id="jdesc" placeholder="Description" style="resize:none;"><?php echo set_value('JDesc');?></textarea>
                    </div>
                 </div>
                 <div class="form-group text-center">
                    <input type="submit" name="btnpost" class="btn btn-green" value="POST JOBS"/>
                 </div>
              </form>
           </div>
        </div>

任何人都可以帮助我,我无法插入数据吗?

2 个答案:

答案 0 :(得分:0)

  1. 尝试不公开制作模型:

    $ query = $ this-&gt; db-&gt; insert(&#39; jobs&#39;,$ data);         if($ query){                 返回true;             } else {                 返回false;             }

  2. 检查表列名称是否与您要插入的内容相匹配,它们可能是错误的...

  3. 使用$this->db->last_query();查看生成查询,并尝试手动插入并运行它,如果失败,您将看到原因:)

答案 1 :(得分:0)

在您的控制器中替换:

$this->Post_Model->insert_postjob($data);

$this->post_model->insert_postjob($data);  // lowercase

在你的模型中添加以下构造函数

$this->load->database(); // if not added in autoload