CodeIgniter Forums
not able to update query by id - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: not able to update query by id (/showthread.php?tid=77637)



not able to update query by id - skghosh - 09-28-2020

I am new in Codeigniter, but when I try to update table value I am getting no error, but data not updated in mysql database. Please someone help.

here is my controller code:

public function changePassword()
{
$this->load->model('User_model');
    $this->logged_in();

    $data['title'] = "Change Password";

    $this->form_validation->set_rules('oldpass', 'Current Password', 'callback_password_check');
    $this->form_validation->set_rules('newpass', 'New Password', 'required');
    $this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[newpass]');

    $this->form_validation->set_error_delimiters('<div class="error">','</div>');


    if($this->form_validation->run() == false){

        $user= $this->session->userdata('user');
        $data['user'] = $user;

    $this->load->view('header', $data);
    $this->load->view('change_password', $data);
    $this->load->view('footer', $data);
}else{

$id = $this->session->userdata('id');
$newpass = $this->input->post('newpass');
       

$this->User_model->update_user($id, array('password' => password_hash($newpass, PASSWORD_BCRYPT))) ;


        redirect('User/logout');

}

}

public function password_check($oldpass)
{

    $id = $this->session->userdata('id');
$user = $this->User_model->get_user($id);


   

  if(password_verify($oldpass, $user['password']) == false){

  //echo '<pre>'; var_dump($user); echo '</pre>';
  //die();

 
    $this->form_validation->set_message('password_check', 'The {field} does not match');

    return false;
    }


    return true;
 
  }


here is my model:

public function get_user($id)
{
    $this->db->where('id', $id);
    $query = $this->db->get('register');
  return $row = $this->db->get('register')->row_array();
 
}

public function update_user($id, $sessArray)
{
$id = $this->session->userdata('id');
//echo '<pre>'; var_dump($id); echo '</pre>';
//die();
  $this->db->set($sessArray);
    $this->db->where('id', $id);
    $this->db->update('register');
 
}


here is my view page:

<div class="row justify-content-center" style="margin-top: 50px;">
    <div class="col-4">
        <h1><?php echo $title ?></h1>
        <?php echo form_open('User/changePassword', array('id' => 'passwordForm'))?>
            <div class="form-group">
                <input type="password" name="oldpass" id="oldpass" class="form-control" placeholder="Current Password" />
                <?php echo form_error('oldpass', '<div class="error">', '</div>')?>
            </div>
            <div class="form-group">
                <input type="password" name="newpass" id="newpass" class="form-control" placeholder="New Password" />
                <?php echo form_error('newpass', '<div class="error">', '</div>')?>
            </div>
            <div class="form-group">
                <input type="password" name="passconf" id="passconf" class="form-control" placeholder="Confirm Password" />
                <?php echo form_error('passconf', '<div class="error">', '</div>')?>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-success">Change Password</button>
            </div>
        <?php echo form_close(); ?>
    </div>
</div>


RE: not able to update query by id - nc03061981 - 09-29-2020

Code:
public function update_user($id, $sessArray)
{
$id = $this->session->userdata('id');
//echo '<pre>'; var_dump($id); echo '</pre>';
//die();
  $this->db->set($sessArray);
    $this->db->where('id', $id);
    $this->db->update('register');

}

Check have data in $id and $sessArray


RE: not able to update query by id - skghosh - 09-29-2020

For $sessArray I am getting the value but for $id NULL value is indicated.


RE: not able to update query by id - InsiteFX - 09-29-2020

Put the id in a hidden input form field then you can grab it from the post array.

PHP Code:
<input type="hidden" name="user_id" value="<?= $user_id; ?>"

Just pass in the the user id:

PHP Code:
$data['user_id'] = $user_id



RE: not able to update query by id - nc03061981 - 09-29-2020

(09-29-2020, 04:44 AM)skghosh Wrote: For $sessArray I am getting the value but for $id NULL value is indicated.

Print
Code:
$user= $this->session->userdata('user');
print_r($user);die();
For checking have $id in session data?


RE: not able to update query by id - skghosh - 09-30-2020

Yes $id value is comming