1
votes

I'm new in codeigniter and php, and am trying to create crud (update) how to solve this? thanks in advance

Model :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Role_model extends CI_Model
{

    public function DeleteRole($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('user_role');
    }

    public function GetId($id)
    {
        return $this->db->get_where('user_role', ['id' => $id])->row_array();
    }


    public function EditRole()
    {
        $data = [

            "role" => $this->input->post('role' , true)
        ];

        $this->db->where('id', $this->input->post('id'));
        $this->db->update('user_role', $data);
    }




}

controller :

    public function __construct()
    {
        parent::__construct();
        is_logged_in();
        $this->load->model('Role_model');
    }

public function edit($id)
{
    $data['title'] = 'Role';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
    $data['user_role'] = $this->Role_model->GetId($id);

    $this->form_validation->set_rules('role', 'Role', 'required');

    if ($this->form_validation->run() == false) {

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('templates/topbar', $data);
        $this->load->view('admin/edit', $data);
        $this->load->view('templates/footer');
    } else {
        $this->Role_model->EditRole();
        $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
        redirect('admin/role');
    } 
}

view :

<div class="card-body">
    <?= $this->session->flashdata('message'); ?>
    <form action="<?= base_url('admin/edit/');?>" method="post">
        <input type="hidden" name="id" value="<?= $user_role['id']; ?>">
        <div class="form-group text-gray-900">
            <label for="role">Edit Role</label>
            <input type="text" class="form-control" id="role" name="Role" value="<?= $user_role['role']; ?>">
            <?= form_error('role', ' <small class="text-danger pl-3">', '</small>'); ?>
        </div>
</div>

and it display like this

An uncaught Exception was encountered Type: ArgumentCountError

Message: Too few arguments to function Admin::edit(), 0 passed in C:\xampp\htdocs\KingflowWP2\system\core\CodeIgniter.php on line 532 and exactly 1 expected

3

3 Answers

2
votes

Let me explain to you.

Edit function needs one parameter (i.e. id). In your form, you are submitting the form without id.

You just need to add the id at the end of the URL like below. Suppose id is 2 then you have to add 2.

base_url('admin/edit/2')

<form action="<?= base_url('admin/edit/2');?>" method="post">
0
votes

You are only passing $id in

$this->usermodel->Role_model($get['id']);
0
votes

You have to change your function if you want to post value in hidden input. Oner more thing you have to use default argument if you are not sure about the data is present or not. Always check step by step that data is present or not than do anything.

Write echo here so you get the value in hidden field

<input type="hidden" name="id" value="<?php echo $user_role['id']; ?>">

After that change in the function:

public function edit()
{
    $id = $this->input->post('id');
    $data['title'] = 'Role';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
    if(!empty($id)):
        $data['user_role'] = $this->Role_model->GetId($id);
    else:
        //some error message 
    endif;
    $this->form_validation->set_rules('role', 'Role', 'required');

    if ($this->form_validation->run() == false) {

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('templates/topbar', $data);
        $this->load->view('admin/edit', $data);
        $this->load->view('templates/footer');
    } else {
        $this->Role_model->EditRole();
        $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
        redirect('admin/role');
    } 
}