【问题标题】:Codeigniter 3: Unknown column 'submit' in 'field list'Codeigniter 3:“字段列表”中的未知列“提交”
【发布时间】:2017-07-24 05:42:34
【问题描述】:

我正在使用 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'

为什么会这样?

【问题讨论】:

  • 你能显示代码以供查看吗?问题是您没有在post 中添加字段名称。
  • 所以$data 应该是一个包含所有表单输入的数组

标签: codeigniter crud


【解决方案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')

【讨论】:

  • 如果这个答案对您有帮助,请支持和支持。 :)
【解决方案2】:

控制器的小改动

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

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2017-11-22
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多