0
我正在嘗試創建一個簡單的註冊表單,但是無法將表單中的數據添加到我的數據庫中?我錯誤地做了什麼,數據不會發布到我的用戶表中?在Codeigniter中沒有插入數據庫的註冊數據?重定向不工作?
另外,如果驗證失敗,又是簡單地加載視圖再次做不當的事情?使用重定向只是失敗,並說我應該嘗試清除我的cookie。
我的代碼如下。
會員註冊控制器:
class Signup extends CI_Controller
{
public function index()
{
$this->load->view('templates/header');
$this->load->view('signup');
$this->load->view('templates/footer');
}
function signup_validation() {
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|min_length[6]|matches[password]');
if ($this->form_validation->run()) {
$this->load->model('signup_model');
redirect('login');
}
else {
$this->load->view('templates/header');
$this->load->view('signup');
$this->load->view('templates/footer');
}
}
}
會員註冊型號:
class Signup_model extends CI_Model {
public function index()
{
$url = url_title($this->input->post('user'), 'dash', true);
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
return $this->db->insert('users', $data);
}
}
會員註冊查看:
<form action="<?php echo site_url('signup/signup_validation') ?>" method="POST">
<h5>First Name</h5>
<input type="text" name="first_name" value="" />
<span><?php echo form_error('first_name'); ?></span>
<h5>Last Name</h5>
<input type="text" name="last_name" value="" />
<span><?php echo form_error('last_name'); ?></span>
<h5>Username</h5>
<input type="text" name="username" value="" />
<span><?php echo form_error('username'); ?></span>
<h5>Email Address</h5>
<input type="text" name="email" value="" />
<span><?php echo form_error('email'); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" />
<span><?php echo form_error('password'); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" />
<span><?php echo form_error('passconf'); ?></span>
<div><input type="submit" value="Register" /></div>
</form>
你叫,你加載模型下的模型,但不是函數嘗試添加'$這個 - > signup_model->指數( );'也https://www.codeigniter.com/user_guide/general/models.html#loading-a-model – user4419336
就是這樣!謝謝! – migs