简体   繁体   中英

Simple CRUD Operation in Codeigniter?

I'm doing simple CRUD operation in Codeigniter.

This is My Controller:

public function areaEdit($area_id)
    {
        $this->global['pageTitle'] = 'Area Edit';
        $data['areaEdit'] = $this->um->areaEdit($area_id);
        $this->loadViews("utilities/area/areaEdit", $this->global, $data , NULL);
    }

    public function updateArea($area_id='area_id')
    {
        $area_name = $this->input->post('area_name');
        $area_abbr = $this->input->post('area_abbr');

        $this->form_validation->set_rules('area_name', 'Area Name', 'trim|required');
        $this->form_validation->set_rules('area_abbr', 'Area Abbrevation', 'trim');

        if ($this->form_validation->run() == FALSE) {
            $this->areaEdit($area_id);
        } else {
            $area_name = ucwords(ucfirst($this->security->xss_clean($area_name)));
            $area_abbr = ucwords(ucfirst($this->security->xss_clean($area_abbr)));

            $data = array('area_name' => $area_name, 'area_abbr' => $area_abbr);

            $result = $this->um->updateArea($data, $area_id);

            if ($result) {
                $this->session->set_flashdata('success', '<b>'.$area_name.'</b> updated successfully on '.date('d/m/Y H:i:s'));
            } else {
                $this->session->set_flashdata('error', 'Something wrong! Please try again.');
            }
            redirect(base_url('utilities/areaList'));
        }
    }

    public function deleteArea($area_id='area_id')
    {
        $result = $this->um->deleteArea($data, $area_id);

        if ($result) {
            $this->session->set_flashdata('success', '<b>'.$area_name.'</b> deleted successfully on '.date('d/m/Y H:i:s'));
        } else {
            $this->session->set_flashdata('error', 'Something wrong! Please try again.');
        }
        redirect(base_url('utilities/areaList'));
    }

And this is My Model is:

public function areaEdit($area_id)
    {
        $this->db->select("*");
        $this->db->from("tbl_area");
        $this->db->where("area_id", $area_id);
        $query = $this->db->get();
        return $query->row();
    }

    public function updateArea($data, $area_id)
    {
        $this->db->where("$area_id", $area_id);
        $this->db->update("tbl_area", $data);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function deleteArea($area_id='area_id')
    {
        $this->db->where("area_id", $area_id);
        $this->db->delete("tbl_area");
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

When I'm running this script, it gives me an error, it goes in else condition in controller for update function and delete function.

I'm not getting that where I'm making mistake ?

Any kind of help is welcome, thanks in advance.

Hope this will help you :

Mismatch arguments : you are passing two parameters from controller but there is only one argument in model's deleteArea() ;

Should be like this :

public function deleteArea($area_id='area_id')
{
   $result = $this->um->deleteArea($area_id);

   if ($result) {
      $this->session->set_flashdata('success', '<b>'.$area_name.'</b> deleted successfully on '.date('d/m/Y H:i:s'));
   } else {
      $this->session->set_flashdata('error', 'Something wrong! Please try again.');
   }
   redirect(base_url('utilities/areaList'));
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM