查询生成器更新() 代码点火器 4 不更新我的密码



>我想问一些问题。我想在代码点火器 4 中更改密码功能,所以我必须更新我的旧密码,但是当我这样做时,密码不会更新,但我所有的 flashdata 都能完美运行。我也尝试了普通的sql查询,但不起作用。 我的错误在哪里?

当我var_dump新的哈希密码时,新密码被哈希,但同样不会更新我的数据库。

这是我的模型

protected $user = 'user';
public function EditKataSandi($password_hash = null){
$session = ConfigServices::session();
$id = $session->get('id');
$db      = ConfigDatabase::connect();
$builder = $db->table($this->user);
//$queryB = "UPDATE `user` SET `sandi` = $password_hash WHERE `id` = $id
//";
// $menu = $db->query($queryB);
// return $menu;
$builder->set('sandi', $password_hash);
$builder->where('id', $id);
return $query = $builder->update();
}

我的控制器

protected $helpers = ['form', 'url', 'array'];
public function katasandi($page = 'katasandi'){
$request = ConfigServices::request();
$validation = ConfigServices::validation();
$model = new Model_all();
$email = $this->session->get('email');
if (!$email){
return redirect()->to(base_url('/auth'));
}else{
$userAccess = $model->Tendang();
if ($userAccess < 1) {
return redirect()->to(base_url('/auth/blokir'));
}
}
if (! is_file(APPPATH.'/Views/admin/admin-katasandi/v_katasandi.php'))
{
// Whoops, we don't have a page for that!
throw new CodeIgniterExceptionsPageNotFoundException($page);
}
if($request->getMethod() == 'post'){
$validation->setRules([
'katasandi_sebelum' => [
'label'  => 'Kata sandi sebelum',
'rules'  => 'required|trim',
'errors' => [
'required' => 'Harus diisi harus diisi!'
]
],
'katasandi_baru' => [
'label'  => 'Sandi Baru',
'rules'  => 'required|trim|min_length[6]|matches[katasandi_baru1]',
'errors' => [
'required' => 'Harus diisi!',
'matches' => '',
'min_length' => 'Terlalu pendek!'
]
],
'katasandi_baru1' => [
'label'  => 'Sandi Ulangi',
'rules'  => 'required|trim|min_length[6]|matches[katasandi_baru]',
'errors' => [
'required' => 'Harus diisi!',
'matches' => 'Harus sesuai dengan kata sandi baru!',
'min_length' => ''
]
]
]);
}
$data['nama'] = $model->GetNama();
$data['title'] = ucfirst('Ubah Kata Sandi'); // Capitalize the first letter
$data['user'] = $model->UserLogin();
$data['menu'] = $model->MenuAll();
$data['attr'] = ['id' => 'katasandi', 'name'=>'katasandi'];
if($validation->withRequest($this->request)->run() == FALSE){
echo view('admin/admin-base-html/v_header', $data);
echo view('admin/admin-base-html/v_navbar', $data);
echo view('admin/admin-base-html/v_sidebar');
echo view('admin/admin-katasandi/v_katasandi', ['validation' => $validation,'session' => $this->session]);
echo view('admin/admin-base-html/v_footer');
echo view('admin/admin-base-html/v_js');
echo view('admin/admin-katasandi/v_js_katasandi');
}else{
$pass_sebelum = $request->getPost('katasandi_sebelum');
$pass_baru = $request->getPost('katasandi_baru');
if (!password_verify($pass_sebelum, $data['user']['sandi'])) {
$this->session->setFlashdata('salah', 'Kata sandi sebelumnya salah!');
return redirect()->to(base_url('/pengguna/katasandi'));
}else{
if ($pass_sebelum == $pass_baru) {
$this->session->setFlashdata('sama', 'Kata sandi baru tidak boleh sama dengan kata sandi sebelumnya!');
return redirect()->to(base_url('/pengguna/katasandi'));
}else{
$password_hash = password_hash($pass_baru, PASSWORD_DEFAULT);
$model->EditKataSandi($password_hash);
$this->session->setFlashdata('pesan', 'Kata sandi berhasil diubah!');
return redirect()->to(base_url('/pengguna/katasandi'));
}
}

}
}

我的观点

<div class="col-sm-12 col-md-12 col-lg-12">
<?php echo form_open(base_url().'/pengguna/katasandi', $attr);    ?>
<?php echo csrf_field(); ?>
<div class="card card-primary">
<div class="card-header">
<h4>Ubah kata sandi</h4>
</div>
<div class="card-body">
<div class="row">
<div class="form-group col-lg-12 col-sm-12 col-md-12">
<label for="katasandi_sebelum">Kata sandi sebelumnya</label>
<input type="password" class="form-control" id="katasandi_sebelum" name="katasandi_sebelum"
placeholder="" autofocus>
<label class="text-danger"><?php echo $validation->showError('katasandi_sebelum') ?></label>
</div>
<div class="form-group col-lg-6 col-sm-12 col-md-6">
<label for="katasandi_baru">Kata sandi baru</label>
<input type="password" class="form-control" id="katasandi_baru" name="katasandi_baru" placeholder="">
<label class="text-danger"><?php echo $validation->showError('katasandi_baru') ?></label>
</div>
<div class="form-group col-lg-6 col-sm-12 col-md-6">
<label for="katasandi_baru1">Ulangi kata sandi baru</label>
<input type="password" class="form-control" id="katasandi_baru1" name="katasandi_baru1"
placeholder="">
<label class="text-danger"><?php echo $validation->showError('katasandi_baru1')?></label>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary"> Ubah Kata Sandi</button>
</div>
</div>
<?php echo form_close(); ?>
</div>

和我的数据库结构

我发现了我的错误,当我将更新密码代码放入新功能时,它的工作......

例:

Public function updatepassword(){
$data['user'] = $model->UserLogin();
$pass_sebelum = $request->getPost('katasandi_sebelum');
$pass_baru = $request->getPost('katasandi_baru');
if (!password_verify($pass_sebelum, $data['user']['sandi'])) {
$this->session->setFlashdata('salah', 'Kata sandi sebelumnya salah!');
return redirect()->to(base_url('/pengguna/katasandi'));
}else{
if ($pass_sebelum == $pass_baru) {
$this->session->setFlashdata('sama', 'Kata sandi baru tidak boleh sama dengan kata sandi sebelumnya!');
return redirect()->to(base_url('/pengguna/katasandi'));
}else{
$password_hash = password_hash($pass_baru, PASSWORD_DEFAULT);
$model->EditKataSandi($password_hash);
$this->session->setFlashdata('pesan', 'Kata sandi berhasil diubah!');
return redirect()->to(base_url('/pengguna/katasandi'));
}
}

使用$allowedFields

应使用可在保存、插入或更新方法期间设置的字段名称更新此数组。除这些字段名称之外的任何字段名称都将被丢弃。这有助于防止仅从表单中获取输入并将其全部扔到模型中,从而导致潜在的批量分配漏洞。protected $allowedFields = ['name', 'email'];

您可以在官方文档中阅读更多信息:https://codeigniter.com/user_guide/models/model.html#models

相关内容

  • 没有找到相关文章

最新更新