Welcome Guest, Not a member yet? Register   Sign In
passing in user id to retrieve user data after login
#1

[eluser]swgj19[/eluser]
I am having a challenging time with this. I created a db table. In that table is an id and a user_id. I am putting together a login password retrieval system. The part I am working on now is the member's logged in page. When the user logs in, I need to be able to retrieve all the users profile data.
I am able only to retrieve the table memberships first row thus far. I need to retrieve the persons data who logs in based on the id passed when they signed up for membership. Currently I know that the id being passed into the form in the view is wrong.

These are the steps that I need to do below:
Add a user_id to your profile table and make it a key.
Then you can grab the users profile by their user id.
So when you create the users profile assign the users id to the profile users id.

Please give me direction in where I am missing it.

Here is my db table:

Code:
CREATE TABLE `membership` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `username` varchar(25) NOT NULL,
  `password` varchar(32) NOT NULL,
  `email_address` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

Here is my new function in my Membership_model to get the user id:

Code:
function retrieve_user($id = '')
     {
     $this->db->where('id', $id);  // get users record
     $q = $this->db->get('membership');   // this would be changed to your users table name

      if($q->num_rows() > 0) {
        // return a single db row array $row['name'];
        return $q->row_array();
        // return a single db object $row->name;
        // return $q->row;      
      }
      return FALSE;
Here is my view. I do not know how to pass the hidden user id when new member is created:

Code:
<h3>Create an Account!</h3>
<fieldset>
<legend>Personal Information</legend>
&lt;?php
  
echo form_open('login/create_member');

echo form_input('first_name', set_value('first_name', 'First Name'));
echo form_input('last_name', set_value('last_name', 'Last Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
?&gt;
</fieldset>

<fieldset>
<legend>Login Info</legend>
&lt;?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', 'Password Confirm');
echo form_hidden('id', $whatever['id']);  //This is where I do not know how to pass in the id

echo form_submit('submit', 'Create Acccount');
?&gt;

&lt;?php echo validation_errors('<p class="error">'); ?&gt;
</fieldset>

Here is my controller. I know I need to pass a variable in my method to pass back into the view to pass into the database. Then I will some how compare the inputed user id to the users user_id.

Code:
function members_profile()
   {
     $this->load->model('Membership_model');
  //pull data
    $data = $this->Membership_model->retrieve_user($id);
    $data = $this
  //load the view and pass the data to it
    $this->load->view('signup_form', $data);
   }
#3

[eluser]swgj19[/eluser]
The net.tutsplus login day 6 tutorial retrieves the user's username by the session user data. I am looking to retrieve the users data in the database by a hidden id field that is passed through when the user signs up. I already know how to set the username in the post equal to the session user data and retrieve the username.

Thanks for your reply.
#4

[eluser]skunkbad[/eluser]
When the user logs in, or when you are checking that they are logged in, this is a good time to retrieve a user ID from the database. I assume when the user logs in, or when you are checking they are logged in when they try to access a page, you are checking the contents of a CI session or cookie. It's at this time when you know who they are, and this is the time to get the user ID.
#5

[eluser]swgj19[/eluser]
You said that I need to grab the user id while I check the session (post username is equal to username)before the user logs in.

Now I realize in order to show the database data after user logs in, I somehow have to retrieve that data. It looks like there are a few steps involved.


These are the steps I need to accomplish:
1. Pass in hidden user_id on my registration form. The id needs to be set to a variable. This is the step I need help on.
2. While I check the session in my controller I need to grab the user_id that was in the form when user registered and compare auto incremented id in database to user_id passed in the form and in database.
3. If user_id = id then grab all of users info for profile from database.

Here is all my code.

Here is my signup form where user_id is passed as a hidden value.
Code:
<h3>Create an Account!</h3>
<fieldset>
<legend>Personal Information</legend>
&lt;?php
  
echo form_open('login/create_member');

echo form_input('first_name', set_value('first_name', 'First Name'));
echo form_input('last_name', set_value('last_name', 'Last Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
?&gt;
</fieldset>

<fieldset>
<legend>Login Info</legend>
&lt;?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', 'Password Confirm');
echo form_hidden('user_id', $post['user_id']);  //Not sure how to accomplish this..

echo form_submit('submit', 'Create Acccount');
?&gt;

&lt;?php echo validation_errors('<p class="error">'); ?&gt;
</fieldset>

The here is my membership_model.php that has create_member function.

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Membership_model extends CI_Model {


function validate()
{
  $this->db->where('username', $this->input->post('username'));  //validate username
  $this->db->where('password', md5($this->input->post('password'))); //validate password
  $query = $this->db->get('membership');        //get db table
  
  if($query->num_rows == 1)           //Make sure data in table exist
  {
   return true;             //if data in table, then return results
  }
}
function create_member()
  {
  $new_member_insert_data = array(         //if validation passed (line 57 controller/login.php), post array data to db
   'first_name' => $this->input->post('first_name'),
   'last_name' => $this->input->post('last_name'),
   'email_address' => $this->input->post('email_address'),
   'username' => $this->input->post('username'),
   'password' => md5($this->input->post('password'))  //make sure password is md5 or same in db
   );
  
   $insert = $this->db->insert('membership', $new_member_insert_data); //insert (insert is new_member_insert_data array) into membership table
   return $insert;
  }
function retrieve()
  {
  $this->db->where('email_address', $this->input->post('email_address'));  //validate username
     $q = $this->db->get('membership');

      if($q->num_rows() > 0) {
        // return a single db row array $row['name'];
        return $q->row_array();
        // return a single db object $row->name;
        // return $q->row;      
      }
      return FALSE;
     function retrieve_user($id = '')
     {
     $this->db->where('id', $id);  // get users record
     $q = $this->db->get('membership');   // this would be changed to your users table name

      if($q->num_rows() > 0) {
        // return a single db row array $row['name'];
        return $q->row_array();
        // return a single db object $row->name;
        // return $q->row;      
      }
      return FALSE;
  }
}
}

Here is my controller that is suppost to retrieve the users id.
Code:
&lt;?php

class Site extends CI_Controller
{
function __construct()
{
  parent::__construct();
  $this->is_logged_in();
}

function members_area()
{
  //load model
    $this->load->model('Membership_model');
  //pull data
    $data = $this->Membership_model->retrieve();
  //load the view and pass the data to it
    $this->load->view('logged_in_area', $data);

   function members_profile()
   {
     $this->load->model('Membership_model');
  //pull data
    $data = $this->Membership_model->retrieve_user($id);
  //load the view and pass the data to it
    $this->load->view('signup_form', $data);
   }
  
  
}
function account()
{
  redirect('site/members_area');
}

function another_page() // just for sample
{
  echo 'good. you\'re logged in.';
}

function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
   echo 'You don\'t have permission to access this page.';
   echo anchor('login/index', 'Login');
   die();  
   //$this->load->view('login_form');
  }  
}

}


#6

[eluser]swgj19[/eluser]
Here is my controller that handles the login functions.

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

function index()
{
  $data['main_content'] = 'login_form';          //create new key 'main_content, which turns into a variable, pass in view you want to load
  $this->load->view('includes/template', $data);        //pass in data array
}

function validate_credentials()
{  
  $this->load->model('Membership_model');          //load membership model
  $query = $this->Membership_model->validate();        //call method
  
  if($query) // if the user's credentials validated...      //if user's credentials are validated...
  {
   $data = array(               //then create new data equal to array
    'username' => $this->input->post('username'),      
    'is_logged_in' => true            //add value to session
   );
   $this->session->set_userdata($data);         //make sure load 'session' in autoload.php or load
                     //set_userdata sets or remembers some string in browser like ip address
   redirect('site/members_area');           //redirect to member's area within site controller if session is true
  }
  else // incorrect username or password
  {
   $this->index();               //load index.php if login was not correct or nothing returned from db
  }
}

function signup()
{
  $data['main_content'] = 'signup_form';          //create new key 'main_content, which turns into a variable, pass in view you want to load
  $this->load->view('includes/template', $data);        //pass in data array
}

function create_member()
{
  $this->load->library('form_validation');         //load form_validation library
  
  // field name, error message, validation rules
  $this->form_validation->set_rules('id','$id','trim|required');
  $this->form_validation->set_rules('first_name', 'Name', 'trim|required'); //field name, error message, validation rules
  $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
  $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
  $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); //4 is minimum length of password
  $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); //match password from previous form box
  
  
  if($this->form_validation->run() == FALSE)
  {
   $this->signup();             //call signup function
  }
  
  else
  {  
   $this->load->model('Membership_model');
  
   if($query = $this->Membership_model->create_member())
   {
    $data['main_content'] = 'signup_successful';     //load signup_successful view
    $this->load->view('includes/template', $data);
   }
   else
   {
    $this->load->view('signup_form');        //direct to signup_form because create member was not successful
   }
  }
  
}

function logout()
{
  $this->session->sess_destroy();
  $this->index();
}



function encrypt()
{
  $this->load->library('encrypt');
  //echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';  //use this line in view to see if mcrypt compatable
}

}
#7

[eluser]Learn CodeIgniter[/eluser]
Instead of returning $insert return $this->db->insert_id(); Now you will have the new users record id and can create their new profile using it.

Code:
function create_member()
  {
  $new_member_insert_data = array(         //if validation passed (line 57 controller/login.php), post array data to db
   'first_name' => $this->input->post('first_name'),
   'last_name' => $this->input->post('last_name'),
   'email_address' => $this->input->post('email_address'),
   'username' => $this->input->post('username'),
   'password' => md5($this->input->post('password'))  //make sure password is md5 or same in db
   );
  
   $this->db->insert('membership', $new_member_insert_data); //insert (insert is new_member_insert_data array) into membership table
   return $this->db->insert_id();
  }

Query Helper Functions

So now the insert_id becomes the users_id and profile_id, do not make the profile_id auto_increment just a key.

Your default charset is incorrect should be
Code:
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#8

[eluser]swgj19[/eluser]
Problem solved!
Thanks Learn Codeigniter. Boy, these new guys can sure teach us how to codeWink
#9

[eluser]Learn CodeIgniter[/eluser]
Your welcome Stephen.




Theme © iAndrew 2016 - Forum software by © MyBB