Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:37:18+00:00 2026-06-11T14:37:18+00:00

I am new to Code Igniter and I’m trying to update the user data

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:37:19+00:00Added an answer on June 11, 2026 at 2:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to code igniter, coming from a solid CakePHP background. The user
I am trying to get the new PDO driver running in Code Igniter 2.1.1
I'm using code igniter and trying to write tests for the data returned by
I'm trying my new hand at Code Igniter and have this issue come up.
I'm new at code igniter and I'm trying to figure out how to utilize
I was having a problem while registering a new user in Code Igniter and
I'm new to code igniter. I'm following the form validation tutorial found at: http://www.codeignitor.com/user_guide/libraries/validation.html
I'm still new to code igniter and I'm having problems getting the login system
I have this function in a Code Igniter model that creates a new video.
I'm new to PHP, started with Code Igniter and MAMP. I start the server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.