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?
What you need to do is pass the third parameter on the
so it will look like this
That third parameter takes an array on string for the where part of the query.
It generates this query:
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 :
Tell me how it goes.