1

I am new to Code Igniter and I'm trying to update the user data from a form. Inserting, displaying and deleting data work fine, but I cannot get the update function to work.

The data are saved in two tables: users and users_info.

My model:

function getuser($id) {
  $this->db->join('users_info','users.user_id = users_info.user');

  $query = $this->db->get_where('users', array('user_id'=> $id));

  return $query->result_array();
 }

function edituser($id) {

  $update_user = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),     
    'email' => $this->input->post('email'),
    'level' => $this->input->post('role')                                       
  );

  $this->db->where('user_id', $id);
  $this->db->join('users_info','users.user_id = users_info.user' );
  $this->db->update('users', $update_user);

  $affected_rows1 = $this->db->affected_rows() ? TRUE : FALSE;

  $update_info = array(
    'name' => $this->input->post('first_name'),
    'surname' => $this->input->post('surname'),
    'address' => $this->input->post('address'),         
    'city' => $this->input->post('city'),
    'phone' => $this->input->post('phone'),
    'mobile' => $this->input->post('mobile'),
    'am' => $this->input->post('am'),           
    'vat' => $this->input->post('vat')                              
  );

  $this->db->where('user', $id);
  $this->db->update('users_info', $update_info); 
  $affected_rows2 = $this->db->affected_rows() ? TRUE : FALSE;

  return ($affected_rows1 && $affected_rows2) ? TRUE : FALSE;

}

My Controller:

function edituser() {
  if ($this->session->userdata('is_logged_in')) {
    $this->load->helper('form');  
    $this->load->helper('html');   
    $this->load->model('users_model');
    $id=$this->uri->segment(3);
    $this->users_model->edituser($id);  

  if ((int)$id > 0) {

    $query = $this->users_model->getuser($id);   

    $data['fid']['value']         = $id;
    $data['fusername']['value']   = $query['username'];
    $data['fpassword']['value']   = $query['password'];
    $data['femail']['value']      = $query['email'];
    $data['frole']['value']       = $query['role'];
    $data['ffirst_name']['value'] = $query['first_name'];
    $data['fsurname']['value']    = $query['surname'];
    $data['faddress']['value']    = $query['address'];
    $data['fcity']['value']       = $query['city'];
    $data['fphone']['value']      = $query['phone'];
    $data['fmobile']['value']     = $query['mobile'];
    $data['fam']['value']         = $query['am'];
    $data['fvat']['value']        = $query['vat'];
  }

  $data['main_content'] = 'pages/edit_user';
  $this->load->view('templates/template', $data);  

  } else {
    redirect('login/index');
  }
}

My view

<div class="form-grid">

  <?php
    $attributes= array('class' => 'leftLabel', 'id' => 'form1');
    echo form_open('users/edituser',  $attributes);
  ?>
  <? echo form_hidden('id',$fid['value']); ?>

  <ul>
    <li>
      <label class="fldTitle">Name<abbr title="Required Field" class="require">*</abbr></label>

      <div class ="fieldwrap">
        <span class="fldcol left">
        <input name="ffirst_name" type="text" tabindex="13" value="<?php echo set_value('ffirst_name',$ffirst_name); ?>"class="full">
        <label class="fldLabel">First Name</label>
        <?php echo form_error('first_name');   ?>   
        </span><span class="fldcol right">
        <input name="fsurname" type="text" tabindex="14" value="<?php echo set_value('fsurname'); ?>" class="full">
        <label class="fldLabel">Surname</label>
        <?php echo form_error('surname');   ?>   
        </span>
      </div>
    </li>
    <li>
      <label class="fldTitle">Login info<abbr title="Required Field" class="require">*</abbr></label>
      <div class ="fieldwrap">

        <span class="fldcol left">
          <input name="username" type="text" tabindex="13"  class="full">
          <label class="fldLabel">Username</label>
          <?php echo form_error('username');   ?>   
        </span>

        <span class="fldcol right">

        <select name="role" tabindex="12" id="combobox" value="<?php echo set_value('role'); ?>">
          <option value="first"<?php echo set_select('role', 'first', TRUE); ?>>First</option>
          <option value="second"<?php echo set_select('role', 'second'); ?>>Second</option>
          <option value="third"<?php echo set_select('role', 'third'); ?>>Third</option>
        </select>

        <label class="fldLabel">Choose user role</label>

        <?php echo form_error('role');   ?> 

        </span><span class="fldcol left">

        <input name="password" type="password" tabindex="13" class="full">
        <label class="fldLabel">Password</label>
        <?php echo form_error('password');   ?> 

        </span> 

        <span class="fldcol right">
          <input name="password2" type="password" tabindex="13" class="full">
          <label class="fldLabel">Comfirm Password</label>
          <?php echo form_error('password2');   ?>   
        </span>
      </div>
    </li>
    <li class="cmplxFld error">
      <label class="fldTitle">User info<abbr title="Required Field" class="require">*</abbr></label>
      <div class ="fieldwrap">
        <span class="full">
        <input name="address" type="text" tabindex="15" value="<?php echo set_value('address'); ?>" class="full">
        <label class="fldLabel">Street Address</label>
        <?php echo form_error('address');   ?>   
        </span>
        <span class="fldcol left">
        <input name="city" type="text" tabindex="17" value="<?php echo set_value('city'); ?>" class="full">
         <label class="fldLabel">City</label>
        <?php echo form_error('city');   ?> 
        </span>
         <span class="fldcol right">
        <input name="email" type="text" tabindex="18" value="<?php echo set_value('email'); ?>" class="full">
        <label class="fldLabel">email</label>
        <?php echo form_error('email');   ?>   
        </span>
        </span>
        <span class="fldcol left">
        <input name="phone" type="text" tabindex="17" value="<?php echo set_value('phone'); ?>"  class="full">
        <label class="fldLabel">Phone</label>
        <?php echo form_error('phone');   ?>   
        </span>
        <span class="fldcol right">
        <input name="mobile" type="text" tabindex="18" value="<?php echo set_value('mobile'); ?>" class="full">
        <label class="fldLabel">Mobile</label>
        <?php echo form_error('mobile');   ?>   
        </span>
        </span>
        <span class="fldcol left">
        <input name="am" type="text" tabindex="17" value="<?php echo set_value('am'); ?>" class="full">
        <label class="fldLabel">AM</label>
        <?php echo form_error('am');   ?>   
        </span>
        <span class="fldcol right">
        <input name="vat" type="text" tabindex="19" value="<?php echo set_value('vat'); ?>" class="full">
        <label class="fldLabel">VAT</label>
        <?php echo form_error('vat');   ?>   
        </span>
      </div>
    </li>
    <li class="buttons bottom-round noboder">
      <div class ="fieldwrap">
        <input name="submit" type="submit" value="Submit" class="submit-button">
      </div>
    </li>
  </ul>

</form>
</div>

Could anyone please tell me what's wrong with my code?

0

2 Answers 2

1

What you need to do is pass the third parameter on the

$this->db->update('users', $update_user);

so it will look like this

$this->db->update('users', $update_user,array('field_name'=>$id));

That third parameter takes an array on string for the where part of the query. It generates this query:

UPDATE users SET (...) WHERE field_name = content_of_$id_variable

EDIT : I re-read you question and noticed that on your first update you added a join clause on it. This code should work.

Model :

function edituser($id) {

    $this->db->trans_start();

    $update_user = array(
        'username' => $this->input->post('username'),
        'password' => md5($this->input->post('password')),     
        'email' => $this->input->post('email'),
        'level' => $this->input->post('role')                                       
    );

    $this->db->update('users', $update_user,array('user_id'=>$id));

    $update_info = array(
        'name' => $this->input->post('first_name'),
        'surname' => $this->input->post('surname'),
        'address' => $this->input->post('address'),         
        'city' => $this->input->post('city'),
        'phone' => $this->input->post('phone'),
        'mobile' => $this->input->post('mobile'),
        'am' => $this->input->post('am'),           
        'vat' => $this->input->post('vat')                              
    ); 

    $this->db->update('users_info', $update_info,array('user',$id)); 

    $status = $this->db->trans_status();

    $this->db->trans_complete();

    return $status;
}

Tell me how it goes.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanx again Ibere. It really solved a database error but I still get this: Message: Trying to get property of non-object... I'm probably doing something wrong with objects and arrays... Please tell me If you have any ideas about it. Thanx again for your time mate
the edituser function returns true or false for the success or failure of the query, what you should do is call $query=$this->users_model->getuser($id); on your controller
Thanx again Ibere. I'm already do this in my controller: $query=$this->users_model->getuser($id); $data['user'] =$query; and use it like this in the view:<input name="username" type="text" tabindex="13" class="full"value="<?php echo $user->username; ?>"> but stil the same error
you getuser method is returning an each row as an array, and also an array of rows, you need to change the return $query->result_array(); to return $query->row(); on the getuser method.
My apologies Ibele I had already made that change but I forgot to mention it. Maybe something is wrong with the query aliases. Thanx again for your help
|
0

Your CI syntax looks correct.

I would check

  1. whether the resulting query is correct. You can use the last_query() function to see it. http://codeigniter.com/user_guide/database/helpers.html
  2. whether required parameters are coming through your form post properly

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.