Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Updating Data, Getting User ID To Be Dynamic?

I wrote syntax allowing my users to update their user information but I’m having a problem. For testing purposes I hard-coded the user_id of 24 into my syntax but I want that user_id to be dynamic. My question is, how would I get my users ID? The below syntax works (I haven’t added form validation but I soon will) when it comes to updating user data I have to dynamically get their ID. Looking at the below snytax how would I go about doing that? Thanks everyone.

My Controller public function update_records(){

 $now = date("Y-m-d H:i:s");

 $form_data = array(
 'business' => $this->input->post('business'),
 'user_id' => $this->input->post('user_id'),
 'address' => $this->input->post('address'),
 'state' => $this->input->post('state'),
 'city' => $this->input->post('city'),
 'zip' => $this->input->post('zip'),
'phone' => $this->input->post('phone'),
'website' => $this->input->post('website'),
'email' => $this->input->post('email'),
'name' => $this->input->post('name'),
'time' => $now
 );

  $this->load->model('Change_data');
  $this->Change_data->update_function_three($form_data); 

My Model

function update_function_three($formdata){
// How would I make '24' dynamic?  
$this->db->where('user_id', '24');
$this->db->update('business',$formdata);
}

Have you tried to access the element in your array? using the syntax for $user_id = $form_data[1]; and then $this->db->where('user_id', $user_id);

Sorry, for that reply the correct way to do this is reference it using the key not the index.

So you answer would be $user_id = $form_data['user_id']; and then use the $user_id variable inside your where call as such : $this->db->where('user_id', $user_id);

Hey Kyle,

Thanks for the response. This syntax works for me. Thanks Kyle for your help.

$this->db->where('user_id', $formdata['user_id']); 

function update_function_three($formdata){

  // $this->db->where('user_id', '24');
  $this->db->where('user_id', $formdata['user_id']); 
      $this->db->update('business',$formdata);

 }

Awesome. I will add my reply to the answers at the bottom then. Glad it worked. PHP can be tricky but its definitely a cool language.

1 Answer

The answer would be

$user_id = $form_data['user_id'];

and then use the $user_id variable inside your where call as such :

$this->db->where('user_id', $user_id);