Codeigniter:如何检查mysql数据库中是否已存在用户名

时间:2016-01-21 06:41:26

标签: php mysql codeigniter

我目前正在开发codeigniter。我不太习惯使用框架。我有一个员工的注册表,我希望它显示" 用户名已存在"在我提交按钮后,在用户名输入旁边。

以下是mysql数据库中表格的图像:

Image of tables from mysql database

这是控制器(home.php):

public function viewAddEmployeeForm() { 
        $this->load->model('Model_home');
        $data = array();
        $data['dropdown'] = $this->Model_home->get_dropdown();
        $this->load->view('imports/header');
        $this->load->view('imports/menu');
        $this->load->view('emp_add', $data);
    }

public function saveEmployee() {
        $this->load->model('Model_home');
        $p = new Model_home();
        $p->date_employed = $this->input->post('date_emp');
        $p->designation_id = $this->input->post('emp_desi');
        $p->username = $this->input->post('username');
        $p->password = $this->input->post('pswrd');
        $p->name = $this->input->post('emp_name');
        $p->midname = $this->input->post('emp_mname');
        $p->lastname = $this->input->post('emp_lname');
        $p->CityAddress = $this->input->post('emp_cadd');
        $p->license_num = $this->input->post('emp_license');
        $p->TIN_num = $this->input->post('emp_tin');
        $p->SSSNo = $this->input->post('emp_sss');
        $p->PhilHealth = $this->input->post('emp_ph');
        $p->DoB = $this->input->post('emp_dob');
        $p->Gender = $this->input->post('emp_gender');
        $p->contnum = $this->input->post('emp_mobno');
        $p->ContactPerson = $this->input->post('emp_contpers');
        $p->ContactPerson_Num = $this->input->post('emp_contpersnum');
        $p->ContactPerson_Add = $this->input->post('emp_contpersadd');

        if($p->designation_id == 1){
            $p->user_type = 0;
        }else{
            $p->user_type = 1;
        }
        $result = $p->saveEmployee();
        if (!$result) {
            echo mysqli_error($result);
        }
        else {
           redirect('home/goSettings', 'refresh');
        }
    }

以下是视图:(emp_add.php):

<h1>Add Employee</h1>
<br>
<?php echo form_open('home/saveEmployee',array('class'=>'form-horizontal'));?>

<h4> Personal Information </h4>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">First Name <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_name" name="emp_name" required="required" class="form-control col-md-7 col-xs-12">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="last-name">Last Name <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_lname" name="emp_lname" required="required" class="form-control col-md-7 col-xs-12">
    </div>
</div>
<div class="form-group">
    <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Middle Name / Initial</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="emp_mname" name="emp_mname" class="optional form-control col-md-7 col-xs-12" type="text">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Gender</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <div id="gender" class="btn-group" data-toggle="buttons">
            <select id="emp_gender" name="emp_gender" class="form-control">
                <option id="emp_gender" name="emp_gender" value="Male">Male</option>
                <option id="emp_gender" name="emp_gender" value="Female">Female</option>
            </select>
        </div>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Date Of Birth <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="emp_dob" name="emp_dob" class="date-picker form-control col-md-7 col-xs-12" required="required" type="date">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Address <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="emp_cadd" name="emp_cadd" class="form-control col-md-7 col-xs-12" required="required" type="text">
    </div>
</div>
<h4> Employee Identification </h4>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">License Number</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_license" name="emp_license" class="optional form-control">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">TIN Number</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_tin" name="emp_tin" class="optional form-control">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Number</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_sss" name="emp_sss" class="optional form-control" >
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">PhilHealth</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_ph" name="emp_ph" class="optional form-control" >
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Username <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="username" name="username" required="required" class="form-control col-md-7 col-xs-12" onblur="return check_username();">
        <div id="Info"></div>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Password <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="password" id="pswrd" name="pswrd" required="required" class="form-control col-md-7 col-xs-12">
    </div>
</div>
<h4> Work details </h4>   
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Date Employed <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="date_emp" name="date_emp" class="date-picker form-control col-md-7 col-xs-12" required="required" type="date">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Designation <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">

        <?php echo form_dropdown('emp_desi', $dropdown, '', 'class="form-control" id="emp_desi"'); ?>   

    </div>
</div>
<h4> Contact Information </h4>  
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Mobile Number <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_mobno" name="emp_mobno" class="form-control" required="required" >
    </div>
</div>
<h4> Contact Person </h4>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Name <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_contpers" name="emp_contpers" required="required" class="form-control col-md-7 col-xs-12">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Address <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_contpersadd" name="emp_contpersadd" required="required" class="form-control col-md-7 col-xs-12">
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Mobile Number <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="emp_contpersnum" name="emp_contpersnum" required="required" class="form-control" >
    </div>
</div>                                  
<div class="ln_solid"></div>
<div class="form-group">
    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
        <button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit" onclick="alert('You have successfully added an employee')">Submit</button>
    </div>
</div>

</form>

这是模型(model_home.php):

public function saveEmployee() {
        if (isset($this->empnum)) {
            $query = $this->updateEmployee();
        }
        else {
            $query = $this->addEmployee();
        }

        return $query;
    }

public function get_dropdown() {
    $result = $this->db->select('designation_id, designation')->get('designation')->result_array();
    $dropdown = array();
    foreach($result as $r) {
        $dropdown[$r['designation_id']] = $r['designation'];
    }
    return $dropdown;
}

3 个答案:

答案 0 :(得分:1)

试试这个

$this->form_validation->set_rules('username', 'Username', 'required|is_unique[users.username]');

此处is_unique[users.username]users指的是用户表&amp; username引用users表中的字段名称。这将自动检查用户表中的用户名。

答案 1 :(得分:0)

要查看数据库中是否已存在某些内容,您通常使用LIKE命令或REGEXP命令来测试该条目是否已存在。有关如何使用REGEXP的信息,请参阅此文章:

mySQL regex in the where clause

如果该项目已经存在,则会返回该项目。如果不是,它应该返回NULL或没有。

然后您只需刷新预先存在的HTML网页即可。您当前网页中唯一需要的更改是一些PHP代码,它只是回显了返回的信息。所以很简单:

<?php echo $foundIt; ?>

你把它放在他们放入用户名的位置旁边(或显示用户名)。如果未找到任何内容,请务必使用空白字段声明变量。即:

$foundIt = "";

否则,您的网页将生成错误消息,说明未定义。

我看到sriAnkush回答了你的问题。我的答案是使用纯HTML。看起来他正在使用CodeIgniter。 : - )

答案 2 :(得分:0)

尝试像这样的控制器功能。获取$username中的用户名并将其传递给模型。使用选择查询,其中username等于$username并返回数组。如果有任何类似的用户名$result将大于或等于1.所以它将显示用户名已经存在,否则它将保存您的数据。

public function saveEmployee() {
  $username=$this->input->post('username');
  $result=$this->model->checkUsername($username);

  if($result>=1){
        echo "Username Already exists";
    }
  else{
      // Query to save the username and data;         
    }
}

祝你好运!