【发布时间】:2018-08-20 01:47:10
【问题描述】:
在我的控制页面中,我需要在我的 usersas6 表中查询所有用户名条目,并与我的视图页面上的表单中的 $username 进行比较。其余的代码都很好。我已经尝试过了,但我似乎没有向 $myerror 变量添加任何内容,即使表中已经有一个带有表单用户名的条目。
public function addPerson()
{
// GET AND SET POSTED DATA
$username = $this->input->post('username');
$password = $this->input->post('password');
$accesslevel = $this->input->post('accesslevel');
$myerror = "";
// add the person to database with
if (strlen($username) < 1)
{
$myerror = "The Username field is required.";
}
$query = $this->db->query("SELECT username FROM usersas6");
foreach ($query->result() as $entry)
{
if ($username == $entry){
$myerror .= "The Username is taken.";
}
}
if(strlen($myerror) != 0){
$this->TPL["myError"] = $myerror;
}
$this->getAllPerson();
$this->template->show('Admin', $this->TPL);
}
【问题讨论】:
-
为什么不使用 where 子句并检查是否有返回行而不是循环所有记录?
标签: php mysql forms codeigniter