0

我在 Codeigniter 中创建了一个表单,但它不会向数据库提交值。我试图通过在我的有效方法中回显“hello”来调试我的代码,但实际方法不会向数据库提交任何内容。

这是我的视图页面中的代码

 <div data-role="content">
            <?php echo form_open('index.php/welcome/adduser'); ?>
            <div data-role="fieldcontain">
            <?php echo validation_errors('<p class="error">','</p>'); ?>
            <p>
            <label>Firstname: </label>
            <?php echo form_input('firstname', set_value( 'firstname' ) ); ?>

            </p>
            <p>
            <label>Lastname: </label>
            <?php echo form_input('lastname', set_value( 'lastname' ) ); ?>

        </p>
        <p>
            <label>Email: </label>
            <?php echo form_input('email', set_value( 'email' ) ); ?>

        </p>
        <p>
            <label>Age: </label>
            <?php echo form_input('age', set_value( 'age' ) ); ?>

        </p>
        <p>
            <label>username: </label>
            <?php echo form_input('username', set_value( 'username' ) ); ?>

        </p>
        <p>
            <label>Password: </label>
            <?php echo form_input('password', set_value( 'password' ) ); ?>

        </p>

        <p>
            <?php echo form_submit('submit', 'Submit'); ?>
        </p>  

    </div> 

这是我的控制器

    <?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /** loading services* */
    function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');
    }

    /** loading several pages * */
    public function index() {
        $this->load->view('abt-header');
        $this->load->view('abt-abovetheblues');
        $this->load->view('abt-footer');
    }

    public function adduser() {


        if ($this->_adduser_validate() === FALSE) {
            $this->index();
            return;
        }
        $data = new user();
        $data->firstname = $this->input->post('firstname');
        $data->lastname = $this->input->post('lastname');
        $data->username = $this->input->post('username');
        $data->password = $this->input->post('password');
        $data->email = $this->input->post('email');
        $data->age = $this->input->post('age');


        $this->load->view('#abt-profile');
    }

    private function _adduser_validate() {

        // validation rules
        $this->form_validation->set_rules('firstname', 'Firstname', 'required');
        $this->form_validation->set_rules('lastname', 'Lastname', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[6]|max_length[12]');

        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[12]');

//        $this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[password]');

        $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
        $this->form_validation->set_rules('age', 'Age', 'required');

        return $this->form_validation->run();
    }



}

?>
4

2 回答 2

0

首先,您还没有关闭表单,请将其添加到您的视图中:

   <?php echo form_close();?>

我看不到您在哪里运行了插入查询,它将在数据库中插入数据。您的 Adduser 函数可以是这样的:

 public function adduser() {


    if ($this->_adduser_validate() === FALSE) {
        $this->index();
        return;
    }
    $data = array(
    'firstname' = $this->input->post('firstname'),
    'lastname' = $this->input->post('lastname'),
    'username' = $this->input->post('username'),
    'password' = $this->input->post('password'),
    'email' = $this->input->post('email'),
    'age' = $this->input->post('age'),
    );

    $this->db->insert('user',$data);
    $this->load->view('#abt-profile');
}

我假设您已经在 config/database.php 文件中设置了数据库名称和登录凭据,这肯定会对您有所帮助。如果有任何问题,请告诉我。

于 2013-08-05T06:01:13.600 回答
0

您没有指定要保存在哪个表中的数据。使用这样的东西...

/*
$data = array(
    'tableCol1' => $this->input->post('postedItem1'),
    'tableCol2' => $this->input->post('postedItem2'),
   );
*/
$this->db->set($data); 
$this->db->insert($tableName);
于 2013-08-04T15:03:23.030 回答