07-04-2020, 09:14 PM
Hi, I have a users table in the database the problem is when I want to update a selected row in the table using save or update function it does not update.
I don't know why but I'm sure that the code is fine.
Below is the function that updates the selected row.
My model
I don't know why but I'm sure that the code is fine.
Below is the function that updates the selected row.
PHP Code:
public function edit_profile()
{
if ($this->request->getPost()) {
$email = $this->request->getPost('email');
$username = $this->request->getPost('username');
$model = new UserModel();
$site_session = new site_session();
$id = session()->get($site_session->userid());
echo $id . ' ', $email . ' ' . $username;
$model->save([
'id' => $id,
'email' => $email,
'username' => $username
]);
}
// return redirect()->to('profile');
}
PHP Code:
<?php
namespace App\Models;
use CodeIgniter\Model;
use site_session;
class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['email', 'username', 'password'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [
'email' => 'required|valid_email',
'username' => 'required|min_length[2]|max_length[10]|is_unique[users.username]',
'password' => 'required|min_length[8]|max_length[20]'
];
protected $validationMessages = [
'email' => [
'required' => 'This field is required',
'valid_email' => 'email is not valid make sure you wrote it right'
],
'username' => [
'required' => 'This field is required',
'min_length' => 'the min username is 2',
'is_unique' => 'this username is takken',
'max_length' => 'the max username is 10'
],
'password' => [
'required' => 'This field is required',
'min_length' => 'the min pasword is 8',
'max_length' => 'the max pasword is 20'
]
];
// protected $skipValidation = false;
protected $beforeInsert = ['hashPassword'];
protected $beforeUpdate = ['hashPassword'];
protected function hashPassword(array $data)
{
if (!isset($data['data']['password']))
return $data;
$password = $data['data']['password'];
$data['data']['password'] = password_hash($password, PASSWORD_DEFAULT);
return $data;
}
public function validateUser($data , $password){
if(password_verify($password , $data)){
return true;
}
if(password_verify($data , $password)){
return true;
}
return false;
}
}