-1

Model

function Updateno($user,$data){
       $this->db->where('username', $user);  
        $this->db->update('user', $data);
    }

Controller

//user login function
    function Ulogin(){

        if($this->session->userdata('user'))
        {
             $data['user'] = $this->session->userdata('user');
            $this->load->view("User/choosecategory",$data);
        }
        else
        {        
        $data = array(
        'username' =>$this->input->post('username'),
        'password' =>$this->input->post('password'),
        );
        $this->form_validation->set_rules('username','UserName','required');
        $this->form_validation->set_rules('password','Password','required');
        if($this->form_validation->run()==false)
            {
            $this->load->view('User/login');
            }
        else
            {
            if($this->user_model->userlogin($data)==false)
            {
                $data['error'] = '<div class="alert alert-danger text-danger">Please Provide Valid Username/Password!</div>';
            $this->load->view('User/login',$data);
            }
            else
            {
               $this->session->set_userdata('user',$data['username']);
                $data['user'] = $this->session->userdata('user');
                $counter = array(
        'logged_in' =>'logged_in'+1
        ); 
        $this->user_model->Updateno($this->session->userdata('user'),$counter);
               $this->load->view("User/choosecategory",$data);
            }
            }
        }
    }

I want whenever user login successfully my logged_in cloumn value increase by 1 but in this code the value of logged_in column always to 1,plz help me anyone here

  • i give it in array like $counter = array( 'logged_in' =>'logged_in'+1 ); in controller in last check – jawahar sharma Apr 11 '17 at 11:18
  • 1
    Use `echo $this->db->last_query();` to print update query and check what it return – Saty Apr 11 '17 at 11:20
  • And change your array to `$counter = array( 'logged_in' =>"'logged_in'+1" ); ` – Saty Apr 11 '17 at 11:23
  • its not working,this chage the value 1 to 0 – jawahar sharma Apr 11 '17 at 11:30
  • @John Conde i understand his problem, He wrote this code on his own, he tried to increment the value - but failed - and he asked for help in this specific matter. i don't even know what's missing here - beside the fact that the post could've a better structure – Atural Apr 11 '17 at 11:31
  • What this return `echo $this->db->last_query();`?? Paste your query – Saty Apr 11 '17 at 11:32
  • **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Apr 11 '17 at 12:14

2 Answers2

2

the only thing you need to do here is to increment the value by 1

try the following piece of code

your model

function Updateno($user)
{
    $this->db->where('username', $user); 
    $this->db->set("logged_in","logged_in+1",false);
    $this->db->update('user');
}

and in your controller

$this->user_model->Updateno($this->session->userdata('user'));
Atural
  • 5,389
  • 5
  • 18
  • 35
0

What you want to do is this:

$value = explode("logged_in", $counter['logged_in']); //get the number of the counter
$counter['logged_in'] = 'logged_in' . ((int)$value[1] +1); //increase value by 1 

This will get the current counter value from your string and increase it by 1

I made a demo to show how you can use it

DEMO: https://eval.in/773121

Shogunivar
  • 1,237
  • 11
  • 18