Welcome Guest, Not a member yet? Register   Sign In
Can't get db->update working
#1

[eluser]linusan[/eluser]
Update 2: I've solved the problem. It think I did wrong by passing two arrays to the where function.

Hi, I'm new to Codeigniter and trying to build a function which changes (updates) the usernames of members. I've read the user guide at least 10 times and searched the forums but can't get it working.

I'm trying to grab the current username from the users session (the login function saves the name into the session array) und pass it to the db->update function. At the same time I'm trying to pass a POST from a form (where the new username has been entered) to the query as well. Then the session username gets changed as well, this seems to work... at least one thing :-) I've set the whole thing into a function which checks if the whole update worked and it returns yes.. so there is no error message at all. Maybe I've messed up the syntax order in the db->update statement?

Here is my controller code:

Code:
function validateName(){
  $new = array(
    'u_name' => $this->input->post('u_name')
   );

  $all = $this->session->all_userdata();
  $where = array(
   'u_name' => $all['u_name']
   );


  $this->load->library('form_validation');

  $this->form_validation->set_rules('u_name', 'Name', 'trim|min_length[4]');
  
  if($this->form_validation->run() == FALSE) {
   redirect('settings/show');
  } else {
   $this->load->model('changename_model');
   if($query = $this->changename_model->changename($new)) {
    //redirect('settings/show');
    echo 'ok';
   }  else {
    //redirect('settings/show');
    echo 'not ok';
   }
  }

Here comes the model:

Code:
function changename($new){

  $new = array(
    'u_name' => $this->input->post('u_name')
   );

  $all = $this->session->all_userdata();
  $where = array(
   'u_name' => $all['u_name']
   );


  $this->db->where('u_name', $where);
  $update = $this->db->update('users', $new);
  return $update;



   $this->session->set_userdata($new);
   redirect('settings/show');
}

Thanks a lot!

Update: I've found an typo where I loaded the model. Now I'm getting an error message but can't figure out to solve it:

Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'Niko' in 'where clause'

UPDATE `users` SET `Nikolaj` = '' WHERE `u_name` = 'Nikolaj' AND `Niko` IS NULL

Filename: /htdocs/findjou/models/changename_model.php

Line Number: 20
#2

[eluser]Otemu[/eluser]
I think you made an error with your update statement, check whether the statement works in mySQL
try the syntax below:

UPDATE [your_table_name] SET [name_of_column]=[new-column_value]
WHERE [user_name_column]=[the_user_name];

Remove [] and replace with your relevant names and values
#3

[eluser]linusan[/eluser]
Thanks a lot! :-)




Theme © iAndrew 2016 - Forum software by © MyBB