我在codeigniter中设计了一个注册页面,但我的表单验证不起作用

时间:2017-08-01 08:49:32

标签: php codeigniter

这是我的代码:

控制器/ register.php

function register() {
   $this->load->view('pages/register');
}

function validation() {
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[register1.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('pages/register');
    } else {
        $data = array(
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'pass' => $this->input->post('password')
        );
        $result = $this->register_m->registration($data);
}

模型/ register_m.php

 function registration($data) {
     foreach($data as $key => $value){
         $a = explode(',', $key)[0];
     }
     $this->db->select('*');
     $this->db->from('register1');
     $this->db->where('email',email);
     $this->db->limit(1);
     $query = $this->db->get();
     if ($query->num_rows() == 0) {
         $this->db->insert('register1', $data);
             if ($this->db->affected_rows() > 0) {
                 return true;
             }
     } else {
         return false;
     }
  }

现在问题是我没有得到如何验证我的电子邮件。这封电子邮件之前是否已经注册。您能否请访问我的代码告诉我在编码过程中是否犯了错误。

2 个答案:

答案 0 :(得分:1)

使用is_unique[register1.email]会为您查找 register1 表格中的出现情况。如果存在,则会标记错误。 请参阅用户指南中的表单验证错误。

所以在你的模型中,你有一个很大的错误......

注意:这是未经测试的

 $this->db->where('email',email); // what is email? a constant??
function registration($data) {
    // Does the email address already exist    
    $this->db->select('*');
    $this->db->from('register1');
    $this->db->where('email', $data['email']); // email is already in your data array
    $this->db->limit(1);
    $query = $this->db->get();
    if ($query->num_rows() == 0) {
        // No so we can save it
        $this->db->insert('register1', $data);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
    } else {
        // The email address already exists so leave
        return FALSE;
    }
}

并且你真的不需要在你的模型中再次检查电子邮件地址,但这样做并没有坏处,这取决于其他人可能称之为...我不知道

更新:需要考虑的事项......当您传入数组时,您应该测试索引,例如' email'实际存在。在数据库更新和插入等中使用它之前,您应该测试所有预期的$ data条目是否存在...虽然在设计时,如果您的$ data条目出错,它将显示,以便您可以修复它。

答案 1 :(得分:1)

因为这个comment代码缩进是个好主意。

对于CodeIgniter中的验证,实现起来非常简单。

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[register1.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

Codeigniter允许您将验证代码放入配置中。 为此,您需要在form_validation.php文件夹中创建config

文件看起来像这样。

$config = array(
        'your_login_validation' => array(
        array(
                'field' => 'name',
                'label' => 'Name',
                'rules' => 'required|trim'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'trim|required|is_unique[register1.email]',
                'errors' => array(
                        'required' => 'You must provide a %s.',                     
                ),
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required'
        )
    )
);

and 


function validation() {
    if ($this->form_validation->run('your_login_validation') == TRUE) {
        // this will directly check the validation and gives the error if fail.         

    } else {
        // To back to view. 
    }

}

我还注意到你正在创建一个post变量数组,分别取每个post数据。所以我建议你使用 $data = $this->input->post();它会立即为您提供所有发布数据。 所以你的控制器看起来像这样:

function validation() {

    if ($this->form_validation->run('your_login_validation') == TRUE) {
        $this->load->view('pages/register');
    } else {
        $data = $this->input->post();
        $result = $this->register_m->registration($data);

        if($result == TRUE){
            // If you are doing login then you can set a session here
        }

    }

}

// Your model code
function registration($data) {

//You don't need to check email exists or not again. Codeigniter validation `is_unique` will take care of it. 

    $this->db->insert('register1', $data);
    // You can use [ $this->db->last_query(); ] to chekc the last query execution for better understanding. 
    // Also you can get last inserted id by using  [ $last_inserted_id = $this->db->insert_id(); ]

    if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
    } else {
        // The email address already exists so leave
        return FALSE;
    }


}

要进行验证,您还可以访问CodeIgniter documentation