Check username exist in database CI/php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

scorpio76
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Jul 30, 2013 6:38 am

Check username exist in database CI/php

Postby scorpio76 » Wed Aug 07, 2013 2:48 am

Anyone can help me? Function to check username that exist not workin. Here the codes;


Modeluser.php

Code: Select all

.  class ModelUser extends CI_Model {

    public function creatAccount()
    {
        $namaStaf   =   $_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->checkUsername($namaStaf) == false)
            return 'Username already exist';

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

    }
   
    public function checkUsername($namaStaf)
    {
        $user_count = $this->db-result("select count(1) as total from akaun where email = 'email'");
        $this->db->select('1');
        $this->db->from('akaun');
        $this->db->where('nama_staf=',"$namaStaf);
        $query=$this->db->get();
        return $query->num_rows() ==  0 ? true:false;
    }
 

Controller file.

Code: Select all

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


scorpio76
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Jul 30, 2013 6:38 am

Re: Check username exist in database CI/php

Postby scorpio76 » Wed Aug 07, 2013 8:55 am

Nobody knows??

User avatar
Strider64
New php-forum User
New php-forum User
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am

Re: Check username exist in database CI/php

Postby Strider64 » Wed Aug 07, 2013 12:48 pm

This might help??? This is how I check to see if a username already exits in a database (I'm using PDO):

Code: Select all

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

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Check username exist in database CI/php

Postby johnj » Fri Aug 09, 2013 9:05 pm

1).What do you mean by "not working"?

2).you should post this to the codeigniter forum


Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests