PHP代码点火器表单验证不起作用
我正在使用PHP代码点火器,在表单验证方面遇到了问题。我遵循了关于CodeIgniter的表单验证教程,但它不起作用。空字段将提交到数据库 我将表单验证规则集添加到控制器中的index函数中 控制器:PHP代码点火器表单验证不起作用,php,forms,codeigniter,validation,Php,Forms,Codeigniter,Validation,我正在使用PHP代码点火器,在表单验证方面遇到了问题。我遵循了关于CodeIgniter的表单验证教程,但它不起作用。空字段将提交到数据库 我将表单验证规则集添加到控制器中的index函数中 控制器: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Phonebook extends CI_Controller { var $TPL; public function
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Phonebook extends CI_Controller {
var $TPL;
public function __construct()
{
parent::__construct();
$this->TPL['newentry'] = false;
$this->load->library(array('form_validation')); // load form lidation libaray & session library
$this->load->helper(array('url','form')); // load url,html,form helpers optional
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$TPL->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
$this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('phonebook_view');
}
else
{
$this->load->view('success_message');
}
}
public function newentry()
{
$fname = $this->input->post("fname");
$lname = $this->input->post("lname");
$phone = $this->input->post("phone");
$email = $this->input->post("email");
$query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");
$this->display();
}
public function addnew()
{
$this->TPL['newentry'] = TRUE;
$this->display();
}
}
如果您将此处的代码与CI userguide中的基本示例进行比较,您会发现这两个示例并不相似。您没有在传入构造函数的数组中加载会话库。请随意从其他方法(即索引
)中删除加载库和帮助程序,因为它们已经加载到构造函数中,并且之后所有类方法中都可以使用相应的对象/函数。作为公共变量的数组,var
是不推荐使用的关键字
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Phonebook extends CI_Controller
{
public $TPL = array();
public function __construct()
{
parent::__construct();
$this->TPL['newentry'] = false;
$this->load->library(array('form_validation, session')); // load form lidation libaray & session library
$this->load->helper(array('url', 'html', 'form')); // load url,html,form helpers optional
}
public function index()
{
$this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
$this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('phonebook_view');
}
else
{
$fname = $this->input->post("fname");
$lname = $this->input->post("lname");
$phone = $this->input->post("phone");
$email = $this->input->post("email");
$query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");
if ((int)$this->db->affected_rows() < 1)
{
print_r($this->db->error());
exit;
}
else
{
redirect('success_page', 'refresh');
//maybe? $this->display();
}
}
public function addnew()
{
$this->TPL['newentry'] = TRUE;
$this->display();
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Phonebook extends CI_Controller
{
public $TPL = array();
public function __construct()
{
parent::__construct();
$this->TPL['newentry'] = false;
$this->load->library(array('form_validation, session')); // load form lidation libaray & session library
$this->load->helper(array('url', 'html', 'form')); // load url,html,form helpers optional
}
public function index()
{
$this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
$this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('phonebook_view');
}
else
{
$fname = $this->input->post("fname");
$lname = $this->input->post("lname");
$phone = $this->input->post("phone");
$email = $this->input->post("email");
$query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");
if ((int)$this->db->affected_rows() < 1)
{
print_r($this->db->error());
exit;
}
else
{
redirect('success_page', 'refresh');
//maybe? $this->display();
}
}
public function addnew()
{
$this->TPL['newentry'] = TRUE;
$this->display();
}
}