我目前正在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;
}
我會在哪裏放? – Jorge
在控制器中 $ this-> form_validation-> set_rules('username','用戶名','required | is_unique [users.username]'); if($ this-> form_validation-> run()){ //您的代碼 } –
請爲我提供完整的mvc代碼。 :( – Jorge