Welcome Guest, Not a member yet? Register   Sign In
[Session] ID of account from accounts table
#1

Hello,

I'm trying to get 'id' value from 'accounts' table and insert it into 'user data' column at 'ci_sessions' table. The thing is it's not working.
I have tried using: $this->session->set_userdata('id', $id); but in 'user_data' field I have inserted "id";N;
How to set id of account logged in?

This is my validate function in login controller:

PHP Code:
public function validate_credentials()
    {
        
$this->load->model('membership_model');
        
$query $this->membership_model->validate();
        
        if (
$query//
        
{
            
$data = array(
                
'name' => $this->input->post('name'),
                
'is_logged_in' => true
            
);
            
$this->session->set_userdata($data);
            
redirect('accounts/');
        }
        else
        {
            
$this->index();
        }
 
   
And here is my membership_model:
PHP Code:
class Membership_model extends CI_Model
{
    function 
__construct()
    {
        
parent::__construct();
    }
    
    function 
validate()
    {
        
$this->db->where('name'$this->input->post('name'));
        
$this->db->where('password'sha1($this->input->post('password')));
        
$query $this->db->get('accounts');
        
        if(
$query->num_rows == 1) {
            return 
true;
        }
    }
    
    function 
create_account()
 
   {
        
$name $this->input->post('name');
        
$email $this->input->post('email');
        
        
$new_account_insert_data = array(
            
'name' => $this->input->post('name'),
            
'password' => sha1($this->input->post('password')),
            
'email' => $this->input->post('email'),
            
'creation' => date('Y-m-d H:i:s'),
        );
        
        
$insert $this->db->insert('accounts'$new_account_insert_data);
        return 
$this->db->insert_id();
 
   

Hope, someone will help me, or give me some tip, what should I do.
Thanks in advance, greetings
qeenyu
Reply
#2

(This post was last modified: 01-24-2015, 12:41 AM by Smith.)

Hi, please make these changes. you have several errors in the code. Before using codeigniter you should learn to program in php

membership_model:
PHP Code:
function validate($name$pass)
 
   {
 
       $this->db->where('name'$name);
 
       $this->db->where('password'$pass);
 
       $this->db->limit(1);
 
       $query $this->db->get('accounts');
 
       
        if
($query->num_rows == 1)
 
       {
 
              return $query->row();
 
       }
 
   

controller:
PHP Code:
$name $this->input->post('name'TRUE);
$password sha1($this->input->post('password'TRUE));

$query $this->membership_model->validate($name$pass);

$data = array(
 
               'name' => $query->name,
 
               'is_logged_in' => true
                
'id'      =>    $query->id,
 
           );
 
           $this->session->set_userdata($data);
 
           redirect('accounts/'); 
Reply
#3

Thank you for help. I didn't realize that this code can be messed.
I got it from yT tutorial, anyways, thank you. Rep++
Reply




Theme © iAndrew 2016 - Forum software by © MyBB