0

我认为这可能是本地服务器问题,因为代码没有问题。如果有人能看一看,将不胜感激!!

控制器:

<?php
class Form1 extends CI_Controller {

    function index() {
        $this->load->view('myform1');
    }

    function save(){
        $this->load->model('form1_model');
        if($this->input->post('submit')){
            $this->form1_model->process();                
        }
        redirect('formsuccess1');
    }
}
?>

模型:

<?php
    class Form1_model extends CI_Model {

        function process(){

             $password = $this->input->post('pass_word');
             $passconf = $this->input->post('password_confirm');
             $email = $this->input->post('e_mail');
             $data = array(
                 'password'=>$password,
                 'passconf'=>$passconf,
                 'email'=>$email 
             );

             $this->db->insert('formdata',$data); 
        }
    }
?>

看法:

<html>
    <head>
        <title>My Form</title>
    </head>

    <body>
        <?php echo form_open('form1/save', array('name' => 'myform')); ?>
        <h5>Password</h5>
        <?php echo form_error('password'); ?>
        <input type="text" name="password"/>
        <h5>Password Confirm</h5>
        <?php echo form_error('passconf'); ?>
        <input type="text" name="passconf"/>
        <h5>Email Address</h5>
        <?php echo form_error('email'); ?>
        <input type="text" name="email"/>
        <div><input type="submit" name="submit" value="Submittt" /></div>
        </form>
    </body>
</html>
4

1 回答 1

1

正如@Joachim 在上面的评论中提到的那样 - 您的帖子变量名称 ( $this->input->post('variable_name');) 需要与您的表单输入元素名称属性相同。

而不是$this->input->post('pass_word');你应该做的$this->input->post('password');

目前,您正试图将一组空值插入数据库。

此外,可能与问题无关,但您不应该直接在模型中访问发布数据,这应该存储在控制器的变量中并作为模型process()函数的参数传递。

这是一个更新的控制器和模型:

<?php
class Form1 extends CI_Controller {

    function index() {
        $this->load->view('myform1');
    }

    function save(){

        // Get your post data here
        $password = $this->input->post('password');
        $passconf = $this->input->post('passconf');
        $email = $this->input->post('email');


        $this->load->model('form1_model');
        if($this->input->post('submit')){

            // Pass the data to your model
            $this->form1_model->process($password, $passconf, $email);                
        }
        redirect('formsuccess1');
    }
}
?>

模型:

<?php
class Form1_model extends CI_Model {

    function process($password, $passconf, $email){

        $data = array(
            'password'=>$password,
            'passconf'=>$passconf,
            'email'=>$email 
        );

        $this->db->insert('formdata',$data); 
    }
}
?>
于 2013-02-07T01:12:38.190 回答