so i’m facing this problem in updating user infos, i’m actually using CI4 framework, so after sending a successful POST request, the request goes OK with Status 200, but nothings really changes in Database. and the data stays as it is.
I literally tried everything using Jquery, fetch, in javascrpt, i did classic updating way, nothing really helped. here is my current code if that would help understanding
this is the controller function im using :
public function edit($id)
{
$userModel = new UserModel();
$user = $userModel->find($id);
if (!$user) {
return redirect()->to('/users')->with('error', 'User not found');
}
if ($this->request->getMethod() === 'post') {
$data = [
'username' => $this->request->getPost('username'),
'first_name' => $this->request->getPost('first_name'),
'last_name' => $this->request->getPost('last_name'),
'email' => $this->request->getPost('email'),
'birthdate' => $this->request->getPost('birthdate'),
'phone' => $this->request->getPost('phone'),
'status' => $this->request->getPost('status'),
'role' => $this->request->getPost('role'),
];
// Use the updateUser method
if ($userModel->updateUser($id, $data)) {
return redirect()->to('/users')->with('success', 'User updated successfully');
} else {
return redirect()->to('/edit_user/' . $id)->with('error', 'Failed to update user');
}
}
return view('admin/edit_user', ['user' => $user]);
}
and this the current view :
<form action="<?= base_url('update/' . $user['id']) ?>" method="POST" class="w-full max-w-lg">
<?= csrf_field() ?>
<div class="mb-4">
<label for="username" class="block text-gray-700 text-sm font-bold mb-2">Username:</label>
<input type="text" id="username" name="username" value="<?= esc($user['username']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="first_name" class="block text-gray-700 text-sm font-bold mb-2">First Name:</label>
<input type="text" id="first_name" name="first_name" value="<?= esc($user['first_name']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="last_name" class="block text-gray-700 text-sm font-bold mb-2">Last Name:</label>
<input type="text" id="last_name" name="last_name" value="<?= esc($user['last_name']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="email" class="block text-gray-700 text-sm font-bold mb-2">Email:</label>
<input type="email" id="email" name="email" value="<?= esc($user['email']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="birthdate" class="block text-gray-700 text-sm font-bold mb-2">Birthdate:</label>
<input type="date" id="birthdate" name="birthdate" value="<?= esc($user['birthdate']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="phone" class="block text-gray-700 text-sm font-bold mb-2">Phone:</label>
<input type="text" id="phone" name="phone" value="<?= esc($user['phone']) ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="status" class="block text-gray-700 text-sm font-bold mb-2">Status:</label>
<select id="status" name="status" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
<option value="Active" <?= $user['status'] == 'Active' ? 'selected' : '' ?>>Active</option>
<option value="Inactive" <?= $user['status'] == 'Inactive' ? 'selected' : '' ?>>Inactive</option>
</select>
</div>
<div class="mb-4">
<label for="role" class="block text-gray-700 text-sm font-bold mb-2">Role:</label>
<select id="role" name="role" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
<option value="User" <?= $user['role'] == 'User' ? 'selected' : '' ?>>User</option>
<option value="Admin" <?= $user['role'] == 'Admin' ? 'selected' : '' ?>>Admin</option>
</select>
</div>
<div class="flex items-center justify-between">
<button type="submit" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Update User
</button>
<a href="<?= base_url('users') ?>" class="inline-block align-baseline font-bold text-sm text-green-500 hover:text-green-800">
Cancel
</a>
</div>
</form>
and this is the function declared in UserModel routing :
public function updateUser($id, $data)
{
$this->set('username', $data['username']);
$this->set('first_name', $data['first_name']);
$this->set('last_name', $data['last_name']);
$this->set('email', $data['email']);
$this->set('birthdate', $data['birthdate']);
$this->set('phone', $data['phone']);
$this->set('status', $data['status']);
$this->set('role', $data['role']);
$this->where('id', $id);
return $this->update();
}
and this is the current routing :
$routes->get('edit_user/(:num)', 'Dashboard::edit/$1', ['filter' => 'authcheck']);
$routes->post('update/(:num)', 'Dashboard::edit/$1', ['filter' => 'authcheck']);