简体   繁体   English

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

[英]Codeigniter: how to check if username already exists from mysql database

I'm currently working on codeigniter. 我目前正在从事Codeigniter。 I am not so expert using framework. 我不是使用框架的专家。 I have a registration form of an employee and I want it to display " username already exists " beside the Username input after I submit a button. 我有一个雇员的登记表,我希望它显示“ 用户名已存在用户名输入旁边后,我提交按钮。

Here is the image of tables from mysql database: 这是mysql数据库表的图像:

Image of tables from mysql database mysql数据库表的图像

Here is the controller (home.php): 这是控制器(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');
        }
    }

Here is the view: (emp_add.php): 这是视图:(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>

Here is the model (model_home.php): 这是模型(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;
}

Try this 尝试这个

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

Here is_unique[users.username] , users refer to users table & username refer to field name in users table. 这里is_unique[users.username]users引用用户表, username引用用户表中的字段名。 This will automatically check for username in users table. 这将自动检查用户表中的用户名。

To see if something already exists in a database you usually either use the LIKE command or a REGEXP command to test if the entry is already there. 要查看数据库中是否已经存在某些内容,通常可以使用LIKE命令或REGEXP命令来测试条目是否已经存在。 See this article for how to use REGEXP: 有关如何使用REGEXP的信息,请参阅本文:

mySQL regex in the where clause where子句中的MySQL正则表达式

If the item is already there it will return the item. 如果该物品已经存在,它将返回该物品。 If not it should return NULL or nothing. 如果不是,则应返回NULL或什么都不返回。

Then all you do is to refresh the pre-existing HTML web page. 然后,您要做的就是刷新预先存在的HTML网页。 The only change you need in your current web page is a bit of PHP code that just echos the returning information. 您当前网页中唯一需要的更改是一些PHP代码,它们仅会回显返回的信息。 So something simple like: 所以简单的东西像:

<?php echo $foundIt; ?>

You put that next to where they put in the username (or the username is displayed). 将其放在用户名(或显示用户名)的旁边。 Be sure to declare the variable with a blank field if nothing is found. 如果未找到任何内容,请确保使用空白字段声明变量。 IE: IE浏览器:

$foundIt = "";

Otherwise your web page will generate an error saying it is undefined. 否则,您的网页将生成一条错误消息,指出它未定义。

I see sriAnkush answered your question. 我看到sriAnkush回答了您的问题。 My answer is using plain HTML. 我的答案是使用纯HTML。 Looks like his is using CodeIgniter. 看起来他正在使用CodeIgniter。 :-) :-)

try controller function like this. 尝试像这样的控制器功能。 Get Username in $username and pass it to model. $username获取Username并将其传递给模型。 use Select query where username is equal to $username and return the array. 使用Select查询,其中username等于$username并返回数组。 If there is any similar username $result will be greater than or equal to 1. so it will show username already exist else it will save your data. 如果有任何类似的用户名, $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;         
    }
}

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM