-2

如何检查用户是否已存在于数据库中。我可以创建用户,但不太确定如何检查用户是否已存在于数据库中。这是我的代码:查看文件

<?php echo $this->navigasi->top(); ?>

<div class="container">
    <br>
    <h4 style="margin:0 auto;width:650px;">CREATE USER ACCOUNT</h4>
    <br>
    <form class="form-horizontal the-form" method="post" action="<?php echo base_url(); ?>admin/register_account">
    <?php echo $this->session->flashdata('mesej'); ?>  
        <div class="control-group">
            <label class="control-label">Name Staff:</label>
            <div class="controls">
                <input type="text" required name="nama_staf" class="input-xlarge">
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">Password:</label>
            <div class="controls">
                <input type="password" required  name="kata_laluan" class="input-xlarge">
            </div>
        </div>

         <div class="control-group">
            <label class="control-label"> Email:</label>
            <div class="controls">
                <input type="email" name="email" class="input-xlarge">
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">Position:</label>
            <div class="controls">
                <select name="jawatan" class="span3" id="jawatan">
                <option value="1">Clerk</option>
                <option value="2">Technician</option>
                <option value="3">Assitant officer</option>
                <option value="4">Officer</option>                  
                <option value="5">Director</option>                 
              </select>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">No. Staff:</label>
            <div class="controls">
                <input type="text" required name="no_staf" class="input-xlarge">
            </div>
        </div>

        <div class="control-group">
            <label class="control-label"></label>
            <div class="controls">
                <button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i> Register</button>&nbsp;&nbsp;

控制器文件

class Admin extends MY_Controller {

public function index()
{
    $session_data = $this->session->userdata('account');
    $data['sesi_jenis'] = $session_data['jenis'];

    if($data['sesi_jenis'] < 1)
    {
        redirect('utama');
    } else {
        $this->load->view('view-utama-pentadbir');
    }
}

public function register()
{
    $this->load->view('view-create-account');
}

public function register_account()
{
    $query = $this->modeluser->createAccount();

    $this->session->set_flashdata('mesej', '<span class="label label-info">Account created!</span> ');
    redirect(base_url().'admin/register');

模型文件

class ModelUser extends CI_Model {

public function creatAccount()
{
    $nameStaf   =   $_POST['nama_staf'];
    $noStaf     =   $_POST['no_staf'];
    $email      =   $_POST['email'];
    $password   =   sha1($_POST['password']);
    $jenis      =   0;  // user is 0 - admin is 1
    $position   =   $_POST['position'];

    $this->db->query("INSERT INTO akaun (nama_staf,no_staf,email,password,jenis,position) VALUES ('$namaStaf','$noStaf','$email','$password','$jenis','$position')");

}

public function padamAkaun($no_staf)
{
    $this->db->query("DELETE FROM akaun WHERE no_staf = '$no_staf'");
}
4

3 回答 3

1
$this->form_validation->set_rules('email', 'Email','trim|required|valid_email|is_unique[tabelname.columnname]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == TRUE)
{
// action after validation success.
}else{
//action after validation failure.
}
于 2013-08-12T05:32:08.473 回答
1
In your controller function set validation rule as:
$this->form_validation->set_rules('email', 'Email', trim|required|valid_email|is_unique[tabelname.columnname]|xss_clean');
于 2013-08-07T05:28:59.760 回答
0

根据每个用户更改模型。考虑到电子邮件对于每个用户都是唯一的

class ModelUser extends CI_Model {

    public function creatAccount()
    {
        $nameStaf   =   $_POST['nama_staf'];
        $noStaf     =   $_POST['no_staf'];
        $email      =   $_POST['email'];
        $password   =   sha1($_POST['password']);
        $jenis      =   0;  // user is 0 - admin is 1
        $position   =   $_POST['position'];
        if ($this->checkEmailExist($email) == false)
            return 'ERROR! DUPLICATE USER';// or handle as you like
        $this->db->query("INSERT INTO akaun (nama_staf,no_staf,email,password,jenis,position)
        VALUES ('$namaStaf','$noStaf','$email','$password','$jenis','$position')");

    }


    public function padamAkaun($no_staf)
    {
        $this->db->query("DELETE FROM akaun WHERE no_staf = '$no_staf'");
    }


   private function checkEmailExist($email)
  {
     $this->db->where('email', $email);
     $query = $this->db->get('akaun');
     if( $query->num_rows() == 0 ){ return TRUE; } else { return FALSE; }
  }
于 2013-08-07T04:46:16.373 回答