未知列' Student_Name'在'字段列表' in codeigniter文件名:E:/xampp/htdocs/codeIgniter/CodeIgniter-3.1.4/system/database/DB_driver.php

时间:2017-05-07 08:04:11

标签: php mysql codeigniter

我正在使用CodeIgniter。我有一个在数据库中输入数据的表单,但我在这里面临一个问题,在codeigniter"中的Student_Name中说'#34;未知列field list。我试过但到目前为止我无法解决。

这是我的代码。

查看文件 Userinsert_view.php

<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>

<div id="container">
<?php echo form_open('Userinsert_controller'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<?php echo form_label('Student Name :'); ?> <?php echo form_error('dname'); ?><br />
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />

<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br />
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?><br />

<?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?><br />
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile', 'placeholder' => '10 Digit Mobile No.')); ?><br />

<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br />
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?><br />

<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">

</div>
</div>
</body>
</html>

控制器文件 Userinsert_controller.php

<?php

class Userinsert_controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Userinsert_model');
    }

    function index() {
        //Including validation library
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        //Validating Name Field
        $this->form_validation->set_rules('dname', 'Username', 'required|min_length[5]|max_length[15]');

        //Validating Email Field
        $this->form_validation->set_rules('demail', 'Email', 'required|valid_email');

        //Validating Mobile no. Field
        $this->form_validation->set_rules('dmobile', 'Mobile No.', 'required|regex_match[/^[0-9]{10}$/]');

        //Validating Address Field
        $this->form_validation->set_rules('daddress', 'Address', 'required|min_length[10]|max_length[50]');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('Userinsert_view');
        } else {
            //Setting values for tabel columns
            $data = array(
                'Student_Name' => $this->input->post('dname'),
                'Student_Email' => $this->input->post('demail'),
                'Student_Mobile' => $this->input->post('dmobile'),
                'Student_Address' => $this->input->post('daddress')
            );
            //Transfering data to Model
            $this->Userinsert_model->form_insert($data);
            $data['message'] = 'Data Inserted Successfully';
            //Loading View
            $this->load->view('Userinsert_view', $data);
        }
    }

}

?>

模型文件 Userinsert_model.php

<?php

class Userinsert_model extends CI_Model{

    function __construct() {
        parent::__construct();
    }

    function form_insert($data){
        // Inserting in Table(students) of Database(college)
        $this->db->insert('students', $data);
    }
}

?>

1 个答案:

答案 0 :(得分:1)

将值传递到所需的数据库列时出错。确保 $ data 数组的密钥在数据库表中具有相同的列名:

 $data = array( 'Student_Name' => $this->input->post('dname'), 'Student_Email' => $this->input->post('demail'), 'Student_Mobile' => $this->input->post('dmobile'), 'Student_Address' => $this->input->post('daddress') );

您正在传递 Student_name 索引中的值,并且该值必须存在于表中。注意区分大小写的列名称。

如果你仍然面临这个问题,请告诉我。