Codeigniter 3:未知列'提交'在'字段列表'

时间:2017-07-24 05:42:35

标签: codeigniter crud

我正在使用Codeignater 3制作CRUD应用程序。

我有一个"添加客户"表格,包括名字,姓氏,电子邮件地址,城市和提交按钮。

该模型如下所示:

class Customer extends CI_Model {

    public function saveCustomer($data) {
        $tbl = $this->db->dbprefix('customers');
        $this->db->insert($tbl, $data);
    }

}

控制器:

if ($this->form_validation->run()) {
        $data = $this->input->post();
        $this->load->model('Customer');
        if ($this->Customer->saveCustomer($data)) {
            $this->session->set_flashdata('response','Customer successfully added');
        } else {
            $this->session->set_flashdata('response','Failed to save customer');
        }
        return redirect('home');
}

查看文件:

<?php echo form_open('home/save'); ?>

    <div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
        <?php echo form_input('first_name', '', [
            'type'  => 'text',
            'id'    => 'first_name', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'First name',
            ]); 
        ?>
        <?php echo form_error('first_name'); ?>                                     
    </div>

    <div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
        <?php echo form_input('last_name', '', [
            'type'  => 'text',
            'id'    => 'last_name', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'Last name',
            ]); 
        ?>
        <?php echo form_error('last_name'); ?> 
    </div>

    <div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
        <?php echo form_input('email', '', [
            'type'  => 'text',
            'id'    => 'email', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'Email address',
            ]); 
        ?>
        <?php echo form_error('email'); ?> 
    </div>

    <div class="form-group">
        <?php echo form_input('phone', '', [
            'type'  => 'text',
            'id'    => 'phone', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'Phone number',
            ]); 
        ?>
    </div>

    <div class="form-group">
        <?php echo form_input('city', '', [
            'type'  => 'text',
            'id'    => 'city', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'City',
            ]); 
        ?>
    </div>

    <div class="form-group">
        <?php echo form_input('address', '', [
            'type'  => 'text',
            'id'    => 'address', 
            'class' => 'form-control',
            'value' => '',
            'placeholder' => 'Address',
            ]); 
        ?>
    </div>

    <div class="form-group">
        <?php echo form_submit('submit', 'Save', 'class = "btn btn-primary btn-block"'); ?>
    </div>

 <?php echo form_close(); ?>

问题: 当我提交表单时,我收到此错误:Unknown column 'submit' in 'field list'

为什么?

2 个答案:

答案 0 :(得分:1)

按如下方式更改$data

$data = array('column_name1' => $this->input->post('first_name'),
              'column_name2' => $this->input->post('last_name'),
              'column_name3' => $this->input->post('email'),
              'column_name4' => $this->input->post('phone'),
              'column_name5' => $this->input->post('city'),
              'column_name6' => $this->input->post('address'));  

注意:控制器中不需要return,只需要redirect('home')

答案 1 :(得分:0)

控制器的微小变化

if ($this->form_validation->run()) {
  $data = $this->input->post();
  //unset your submit value which are come from submit btn
  if(isset($data['submit'])){unset($data['submit'])}
  if(isset($data->submit)){unset($data->submit)}
  $this->load->model('Customer');
  if ($this->Customer->saveCustomer($data)) {
    $this->session->set_flashdata('response','Customer successfully added');
  } else {
    $this->session->set_flashdata('response','Failed to save customer');
  }
  return redirect('home');
}