如何验证字段

时间:2016-12-19 10:29:41

标签: codeigniter

我想将验证数据插入数据库 我的代码如下所示,其中submit是我的按钮名称和名称,email,mobile是我的表单字段:

if($this->input->post('submit'))
{
    $name = $this->input->post('name');
    $email = $this->input->post('email');
    $mobile = $this->form_validation->set_rules('mobile', 'Mobile','trim|required|numeric');
    $query = $this->my_model->insertdata($name, $email, $mobile);
}

如果我编写了类似上面的代码,如何验证表单字段?

3 个答案:

答案 0 :(得分:0)

您应该使用Form Validation Library

首先,自动加载它:

打开application/config/autoload.php并编辑第61行,如下所示:

$autoload['libraries'] = array('form_validation');

现在,您可以在控制器中验证表单。以下是您的示例:

public function some_function()
{
  $this->form_validation->set_rules('input_name', 'input human name', 'required');

 if ($this->form_validation->run() === false)
 {
   //return some error here
 }
 else 
 {
  // send data to your model (e.g. $this->Some_model->some_function())
 }
}

请参阅Form Validation Library Guide以查看所有参考资料和示例。此外,请按照此顺序提交表单:查看 - >控制器 - >模型。

答案 1 :(得分:0)

首先,您将不安全的数据放入数据库。

UPDATE your_table_name
SET updated_column_name = (
    DATE_ADD(NOW(), INTERVAL 20 MINUTE)
);

答案 2 :(得分:0)

public function register()
{
  $this->form_validation->set_rules('username', 'Usename Name', 'trim|required|min_length[3]|max_length[30]');
  $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[jobseeker_registration.email]');
  $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|numeric');


if ($this->form_validation->run() === false)
   { $this->load->view('register'); }
else 
 {
  $data = array(
                'name' => $this->input->post('name'),
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'mobile' => $this->input->post('mobile'),
                'password' => $this->input->post('password')
            );

$this->Jobseeker_model->insertdata($data)
 }
}

此代码对我来说非常适合表单验证