php - Codeigniter: how to check if username already exists from mysql database -


i'm working on codeigniter. not expert using framework. have registration form of employee , want display "username exists" beside username input after submit button.

here image of tables mysql database:

image of tables mysql database

here controller (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 view: (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 added employee')">submit</button>     </div> </div>  </form> 

here model (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 $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 users table & username refer field name in users table. automatically check username in users table.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -