Singam2 Posted August 9, 2013 Share Posted August 9, 2013 "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'));" Quote Link to comment Share on other sites More sharing options...
boompa Posted August 9, 2013 Share Posted August 9, 2013 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. Quote Link to comment Share on other sites More sharing options...
Singam2 Posted August 10, 2013 Author Share Posted August 10, 2013 so how should it be in the code? Quote Link to comment Share on other sites More sharing options...
Strider64 Posted August 10, 2013 Share Posted August 10, 2013 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. } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.