No database update after successful POST request in Codeigniter4

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']);
1 Like

It looks like your code structure is mostly correct, but the issue might be with the way the update is being handled in the UserModel. Let’s go through the process and see where potential problems might be and how to fix them.

Potential Issues:

  1. Update Method Return Value: The update() method in the model should take the ID and the data array as parameters directly. The way it’s currently being used might not be updating the data correctly.
  2. Model Declaration: Ensure that your model is correctly configured and extends CodeIgniter\Model.
  3. Form Action URL: Ensure that the form action URL is correct and pointing to the right endpoint.

Suggested Fixes:

  1. Update the updateUser Method: Instead of setting each field individually, you can pass the whole array to the update() method directly. The update() method should be used like this:
public function updateUser($id, $data)
{
    return $this->update($id, $data);
}

  • This will update the user record with the specified ID using the data provided.
  • Controller edit Method: Make sure the updateUser method is correctly used in your controller:
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'),
        ];

        // Call the updated 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]);
}

  • Check Database Changes: After making these changes, ensure that your form is submitting correctly by checking the network tab in your browser’s developer tools. Verify that the POST request is sending the expected data and that the database reflects these changes.
  • Debugging: If the issue persists, try logging the SQL query executed or adding some debug statements to see what data is actually being passed to the model. You can use log_message('info', 'Data: ' . print_r($data, true)); to log the data you’re trying to update.