Php I';我在codeigniter中设计了一个注册页面,但是我的表单验证不起作用
这是我的密码: controller/register.phpPhp I';我在codeigniter中设计了一个注册页面,但是我的表单验证不起作用,php,codeigniter,Php,Codeigniter,这是我的密码: controller/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('
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);
}
models/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;
}
}
现在的问题是我不知道如何验证我的电子邮件。此电子邮件之前已注册或未注册。您还可以访问我的代码,告诉我我在编码过程中是否犯了错误。使用
是唯一的[register1.email]
将在您的注册表1表中查找发生的情况。如果它存在,它将标记一个错误请参阅《用户指南》中的表单验证错误。
所以在你的模型中,你犯了一个很大的错误
注意:这是未经测试的
而且你不需要在你的模型中再次检查电子邮件地址,但是这样做是无害的,这取决于其他的称呼。。。我不知道
更新:需要考虑的事情…当您传入数组时,您应该真正测试像“email”这样的索引是否确实存在。在数据库更新和插入等中使用之前,您应该测试所有预期的$data条目是否存在。。。虽然在设计时,如果您的$data条目出错,它会显示出来,以便您可以修复它。
使用是唯一的[register1.email]
将为您在register1表中查找发生的情况。如果它存在,它将标记一个错误请参阅《用户指南》中的表单验证错误。
所以在你的模型中,你犯了一个很大的错误
注意:这是未经测试的
而且你不需要在你的模型中再次检查电子邮件地址,但是这样做是无害的,这取决于其他的称呼。。。我不知道
更新:需要考虑的事情…当您传入数组时,您应该真正测试像“email”这样的索引是否确实存在。在数据库更新和插入等中使用之前,您应该测试所有预期的$data条目是否存在。。。虽然在设计时,如果您的$data条目出错,它会显示出来,以便您可以修复它…
,因为这个代码缩进将是一个好主意 对于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将允许您将验证代码放入配置中。
为此,您需要在config
文件夹中创建form\u validation.php
文件看起来像这样
$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()代码>它将一次为您提供所有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中的验证,实现起来非常简单
$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将允许您将验证代码放入配置中。
为此,您需要在config
文件夹中创建form\u validation.php
文件看起来像这样
$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()代码>它将一次为您提供所有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 2或3?好的,我会的,但您能否告诉我此代码中的错误,或者告诉我如何验证电子邮件是否在注册之前注册?@JeroenvanVeghel我使用Codeigniter3@RiggsFolly你能告诉我怎么得到这封电子邮件吗我输入了模型?一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。您可能会被要求在几周/几个月内修改此代码,最终您会感谢我。您是否使用Codeigniter 2或3?好的,我会的,但您能否告诉我此代码中的错误,或者告诉我如何验证电子邮件是否在注册之前注册?@JeroenvanVeghel我使用Codeigniter3@RiggsFolly你能告诉我怎么得到这封电子邮件吗我进入了模型?非常感谢你的帮助和良好的解释。这很有效:)我确实在结尾添加了一个小注释,只是想一想!是的,我检查了我的$data数组,以确认我的数据是否存储在此数组中再次感谢您的建议非常感谢您的帮助和良好的解释。这是有效的Grand:)我确实在结尾添加了一点注释,只是想一想!是的,我在检查我的$data数组之前确认我的数据是否存储在此数组中再次感谢您的建议非常感谢您的帮助我已经解决了我的问题但感谢您的时间和您的好意我已经更新了答案,并提供了一些改进代码的建议:)@ranaaqibjaved非常感谢您的帮助我已经解决了我的问题,但感谢您的时间和好意,我已经更新了答案,并提供了一些改进代码的建议:)@ranaaqibjaved