0

我是 CodeIgniter 的新手。

目前我有一个 register.php(VIEW) 和一个 register.php(CONTROLLER)。register.php(VIEW) 包含一个简单的表单,我试图从这个表单将数据传递给控制器​​,以便将其插入数据库。简单的。

每当我加载视图时,我似乎得到的只是与变量和函数有关的不同错误消息,还有我尝试加载视图的行上的错误。

我只是问:

  1. 这是一起使用控制器和视图的正确方法吗?
  2. 我该如何解决这个当前的问题?

下面是两个文件:

register.php (查看)

<htmL>

<body>

<form method="post" action="controllers/register.php">

<input type="text" name="email">
<input type="text" name="name">
<input type="password" name="password">
<select id="userLevel" name="userLevel">        
    <option value="2">Job Seeker</option>
    <option value="3">Employer</option>
</select>
<input type="submit" name="submit" value="Submit">

</form>

</body>

</htmL>

register.php(控制器)

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

class Users extends CI_Controller{  

public function __construct(){

    public $email;
    public $name;
    public $password;
    public $userLevel;

    $this->email = $_POST['email'];
    $this->name = $_POST['name'];
    $this->password = $_POST['password'];
    $this->userLevel = $_POST['userLevel'];

}

public function createuser() {

    if( filter_var($this->email, FILTER_VALIDATE_EMAIL) ) {

        $this->db->set('email', $this->email);
        $this->db->set('name', $this->name);
        $this->db->set('password', $this->password);
        $this->db->set('userLevel', $this->userLevel);
        $this->db->insert('users');

    }   

}

$this->load->view('register');

}

?>
4

2 回答 2

3

您的控制器应如下所示:

class Users extends CI_Controller{  
public function __construct(){
    parent::__construct();
}

public function createuser() {
    if($this->input->post(null)){
        $data   = array();
        $data['email']      = $this->input->post('email');
        $data['name']       = $this->input->post('name');
        $data['password']   = $this->input->post('password');
        $data['userLevel']  = $this->input->post('userLevel');
        $this->db->insert('users', $data);
    }
    $this->load->view('register');
}   
}

而不是<form method="post" action="controllers/register.php">写写<?=form_open('user/createuser')?>

于 2013-07-26T10:48:57.353 回答
2

你需要这样做。它是 MVC 并且更清晰

看法

<htmL>

<body>

<?php echo form_open('controllers/createuser'); ?>
<input type="text" name="email">
<input type="text" name="name">
<input type="password" name="password">
<select id="userLevel" name="userLevel">        
    <option value="2">Job Seeker</option>
    <option value="3">Employer</option>
</select>
<input type="submit" name="submit" value="Submit">

<?php echo form_close(); ?>

</body>

</htmL>

控制器

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

    class Users extends CI_Controller{  

    public function __construct(){
        $this->load->helper('form');

    }

    public function createuser() {

        $data['email'] = $this->input->post['email'];
        $data['name'] = $this->input->post['name'];
        $data['password'] = $this->input->post['password'];
        $data['userLevel'] = $this->input->post['userLevel'];

        if($this->input->post('submit')) {

            // Here you can validate data if you want

            $this->user_model->insert($data);
            redirect('users/success');
        }
        $this->load->view('register');
    }

    function success() {
      echo "You add user";
    }



}

    ?>

模型

class User_model extends CI_model {

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

    function add($data) {
        $this->db->insert('users', $data);  // table name users
    }
    }
于 2013-07-26T10:55:44.987 回答