Welcome Guest, Not a member yet? Register   Sign In
not able to update query by id
#1

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>
Reply
#2

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

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#3

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

(This post was last modified: 09-29-2020, 11:20 AM by InsiteFX.)

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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?

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#6

Yes $id value is comming
Reply




Theme © iAndrew 2016 - Forum software by © MyBB