Jump to content

Database username check


Singam2

Recommended Posts

 

"Can anyone help me on this codes? I want a flash message displays

User account already exist! When the same no_ staf used to create new user account.

So far this is the codes i have. If the user exist, flash message appear else account is created an stored in database. Thank u

 

Controller file

public function daftar_akaun()

 

{

 

$query = $this->db->select('no_staf')->from('akaun')->where('no_staf',$noStaf)->get();

 

if( $query->num_rows() > 0 ){ return TRUE; } else { return FALSE; }

 

$query = $this->modeluser->ciptaAkaun();

 

$this->session->set_flashdata('mesej', '<span class="label label-info">Account created</span> ');

redirect(base_url().'admin/daftar');

}

 

model file

U

public function ciptaAkaun()

{

 

$namaStaf = $_POST['nama_staf'];

$noStaf = $_POST['no_staf'];

 

 

 

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

Link to comment
Share on other sites

Please use code tags when posting code.

 

 

public function daftar_akaun()
{
    $query = $this->db->select('no_staf')->from('akaun')->where('no_staf',$noStaf)->get();

    if( $query->num_rows() > 0 ){ return TRUE; } else { return FALSE; }
 
    $query = $this->modeluser->ciptaAkaun();

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

 

You're returning from the method (which may not even be the right thing to in a controller method, but there's not enough context) as soon as you check the number of rows. You never reach the point of setting the flash method and redirecting.

Link to comment
Share on other sites

I don't know how you have your database setup, but all you have to do is check to see if the person exists in the table and return a true value. Then you can just simply use an if-statement sending a message to the user.

 

For example here is how I do something similar:

    // Method checks to see if username isn't already taken and returns true if it is already taken:
    public function isUsernameAvailable() {

        // Connect to PDO database:
        $db = Database::getInstance();
        $pdo = $db->getConnection();

        $query = "
            SELECT
                1
            FROM users
            WHERE
                username = :username1
        ";

        $query_params = array(
            ':username1' => $this->username
        );

        // These two statements run the query against your database table.
        $stmt = $pdo->prepare($query);
        $result = $stmt->execute($query_params);

        // The fetch() method returns an array representing the "next" row from
        // the selected results, or false if there are no more rows to fetch.	   	   
        return $row = $stmt->fetch();
        // If a row was returned, then we know a matching username was found in
        // the database already and we should return a boolean value back.	   
    }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.