CodeIgniter Forums
experts help me on this code.. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: experts help me on this code.. (/showthread.php?tid=58979)



experts help me on this code.. - El Forum - 08-11-2013

[eluser]bujal[/eluser]
I would like to check if user exist in the database when creating new account. with this codes when I try to create user which is already exist in the database, the message user already exist doesn't appear. instead the flash message displayed saying that account is created. But nothing stored in the database.I m not sure where is the error. this is 1st time using codeigniter, appreciate help from experts..thanks
How if I want to check both $nameStaf & $noStaf already exist??
Code:
controller codes

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

Code:
class ModelUser extends CI_Model {

    public function createAccount()
    {
        $nameStaf   =   $_POST['nama_staf'];
        $noStaf     =   $_POST['no_staf'];
        
        if ($this->checkUser_exist($nameStaf) == false)
            return  USER ALREADY EXIST';

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

    }

Code:
private function checkUser_exist($nameStaf)
  {
     $this->db->where('nama_staf'', $nameStaf );
     $query = $this->db->get('akaun');
     if( $query->num_rows() == 0 ){ return TRUE; } else { return FALSE; }
  }



experts help me on this code.. - El Forum - 08-11-2013

[eluser]bujal[/eluser]
well, is this question too easy for u all since nobody replies Smile


experts help me on this code.. - El Forum - 08-11-2013

[eluser]bujal[/eluser]
Ghis place really sucks.. Nobody to help huh


experts help me on this code.. - El Forum - 08-11-2013

[eluser]sv3tli0[/eluser]
The best advice is to check the userguide of CI
http://ellislab.com/codeigniter/user-guide/

In your case check the usage of the flashdata method Smile
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
You have to set "$this->session->flashdata('item');" inside your View to see the message which you have set (error or success) Smile

+ you may use Form_validation to grab POST data and to check a field exists in your DB with is_unique check .
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html