检查ID或任何列值不存在数据库中的CI有这方面的验证规则。
看到它住在这里:Validation Rule
规则:如果表单元素不是唯一在参数表和字段名is_unique
返回FALSE。注意:此规则要求启用查询生成器才能工作。
例子:is_unique[table.field]
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
对于更先进的使用验证,您可以添加所有验证设置规则使用数组。
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
$config = array(
'your_rule_name' => array(
array(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
)
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);
您是否检查过运行此命令时是否存在'$ q'? – matthewpavkov 2013-04-20 21:51:59
$ q检查数据库中是否存在任何id,因为它不会向数据库中插入任何新信息 – Hashey100 2013-04-20 21:55:25
正确,但我会验证$ q实际上是“false”。试试'if($ q-> num_rows()> 0){...}'然后在你的'if'中做一个'echo',这样你就可以看到发生了什么。 – matthewpavkov 2013-04-20 22:03:27