檢查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