2016-01-21 157 views
0

我目前正在codeigniter上工作。我不擅長使用框架。我有一個員工的註冊表,我希望它顯示「用戶名已存在」旁邊的用戶名輸入後,我提交了一個按鈕。Codeigniter:如何檢查用戶名是否已經存在從MySQL數據庫

下面是表從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; 
} 

回答

1

試試這個

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

這裏is_unique[users.username]users指用戶表& username指字段名的用戶表。這將自動檢查用戶表中的用戶名。

+0

我會在哪裏放? – Jorge

+0

在控制器中 $ this-> form_validation-> set_rules('username','用戶名','required | is_unique [users.username]'); if($ this-> form_validation-> run()){ //您的代碼 } –

+0

請爲我提供完整的mvc代碼。 :( – Jorge

0

要查看數據庫中是否存在某些內容,通常要麼使用LIKE命令,要麼使用REGEXP命令來測試該條目是否已經存在。請參閱本文如何使用REGEXP:

mySQL regex in the where clause

如果該項目已經存在,將返回該項目。如果不是,它應該返回NULL或什麼都不返回

然後你要做的就是刷新預先存在的HTML網頁。您在當前網頁中唯一需要修改的部分PHP代碼只是迴應了返回的信息。所以簡單的事情如下:

<?php echo $foundIt; ?> 

你把那個放在他們放在用戶名(或用戶名顯示)旁邊。如果沒有發現任何內容,請務必使用空白字段聲明該變量。 I.E .:

$foundIt = ""; 

否則你的網頁會產生一個錯誤,說它是未定義的。

我看到sriAnkush回答了你的問題。我的答案是使用純HTML。看起來他正在使用CodeIgniter。 :-)

0

嘗試像這樣的控制器功能。在$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;   
    } 
} 

祝你好運!

相關問題