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
https://forums.phpfreaks.com/topic/280999-database-username-check/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.