Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 3: update Sql table column via GET
#1

I am working on a Register and Login application with CodeIgniter 3 and Bootstrap.

In my "users" table I have an "active" column that can take either 0 or 1 as value.

[Image: 6aqS9.png]

I want to be able to change the value of the "active" column corresponding to a user from 0 to 1 (activate the user) by clicking a link in my users view:

Code:
<a href="<?php echo base_url(); ?>users/activate/<?php echo $user->id ?>" title="Activate" class="btn btn-success btn-xs activate-btn"><span class="glyphicon glyphicon-ok"></span> Activate</a>


In my Usermodel model I have:

Code:
public function activateUser($user_id) {
   $query = $this->db->get_where('users', ['id' => $user_id]);
   return $query->row();
}


In my User controller I have:

Code:
public function activate($user_id) {
   $this->load->model('Usermodel');
   $user = $this->Usermodel->activateUser($user_id);
   if ($user->active == 0) {
       echo 'activate user';
   }   else {
       echo 'user already active';
   }
}


The url users/activate/1 returns "user already active" , while users/activate/2 returns "activate user", as expected. Being new to Codeigniter, I have tried numerous versions of the code above that resulted in errors.

Can you please tell me what shall I change in the code to make work as desired?
Reply
#2

As far I can tell, you only forgot to change the data. Replace your 'activate user' part of the code with a usermodel that actually changes the table.

PHP Code:
$data = array('active' => 1);
$this->db->update('users'$data, array('id' => $user_id)); 
You also need to check that the current user have the right privileges, so that only you (or an admin) can do it.
Reply
#3

(01-14-2018, 01:34 PM)jreklund Wrote: As far I can tell, you only forgot to change the data. Replace your 'activate user' part of the code with a usermodel that actually changes the table.

PHP Code:
$data = array('active' => 1);
$this->db->update('users'$data, array('id' => $user_id)); 
You also need to check that the current user have the right privileges, so that only you (or an admin) can do it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB