-2

How do i check if the user already exist in the database. I can create user but not very sure how to check if the user already exist in the databas. here are my code: view file

<?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;

controller file

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');

model file

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'");
}
3
  • you can perform a query with the given username and email before insert Commented Aug 7, 2013 at 4:07
  • Try checking with email id.If email id exists then say user alderady exists. Commented Aug 7, 2013 at 4:07
  • THATS MEAN I SHOULD CREATE CODE WITH FORM VALIDATION BEFORE INSERT IN MODEL?? Commented Aug 7, 2013 at 4:33

3 Answers 3

0
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');
Sign up to request clarification or add additional context in comments.

1 Comment

i m still in blur...many attempt does not work...pls help me with correct codes...only this is my problem running the system..thank u very much
0

change model as per.considering email is unique for each user

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; }
  }

7 Comments

thic code received error on line $user_count...fatal error:call to undefined function result...i prefer to use username instead of email id
i dont have code igniter right now. Please use result_array funnction. weather it helps. or just search out
hi susheel...thanks for your correction but u can u make it work for me??
try now, code improved. check this url also stackoverflow.com/questions/4296930/…
no error displayed when running the system, but wheni try to register same email address, the system automatically adjust the email like this; if the existing email is [email protected] and i try register the same email it drop one letter like this [email protected] and creates new account.
|
0
$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.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.