简体   繁体   中英

Unable to update or delete rows in database using codeigniter

I am new to code-igniter, and I am facing unknown issues in updation and deletion of rows in my database . My code for Controller is :

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

class Nhome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->Model('N_model');
        $data['r'] = $this->N_model->getdata();
        $this->load->view('Homeview',$data);
    }
    public function edit()
    {
        $id = $this->input->get('id');
        $this->load->Model('N_model');
        $data['s'] = $this->N_model->editdata();
        $this->load->view('Neditview',$data);
    }
    public function loadEdit()
    {
        $id = $this->input->get('id');
        $this->load->view('Neditview');
    }
    public function insertdata()
    {
        $eID = isset($_POST['Id'])?$_POST['Id']:'';

        $arr['Name'] = $_POST['Name'];
        $arr['Gender'] = $_POST['Gender'];
        $arr['Email'] = $_POST['Email'];

        $this->load->Model('N_model');
        $res = $this->N_model->updatedata($arr , $eID);
        if($res){
            header('location:'.base_url()."index.php/Nhome/".$this->index());
        }
    }
    public function delete(){
        $this->load->Model('N_model');
        $id = $this->input->get('Id');
        $this->N_model->deletedata($id);
        $this->index();
    }
}

and my code for model is :

<?php
class N_Model extends CI_Model{

    public $Id;
    public $Name;
    public $Gender;
    public $Email;

    public function __construct()
    {
        parent::__construct();
    }

    public function getdata()
    {
        $va = $this->db->get('newprac');
        $res = $va->result();
        return $res;
    }
    public function editdata($id)
    {
        $vr = $this->db->where('Id',$id);
        return $vr;
    }
    public function updatedata($data , $id){
        $this->db->where('newprac.Id',$id);
        $res = $this->db->update('newprac', $data);
        return $res;
    }
    public function deletedata($id)
    {
        $this->db->where('newprac.id',$id);
        $this->db->delete('newprac');
        if($this->db->affected_rows()>0)
        {
            return true;
        }
        else { return false; }
    }
}

Change

$this->db->where('newprac.id',$id)

to

$this->db->where('id',$id);

将代码简化为:

$this->db->where('id',$id)->update('newprac', $data);

Replace your controller by this code. Becuase I think you passing data as a post method so you should use post when you retrieve data.

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

class Nhome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->Model('N_model');
        $data['r'] = $this->N_model->getdata();
        $this->load->view('Homeview',$data);
    }
    public function edit()
    {
        $id = $this->input->post('id');
        $this->load->Model('N_model');
        $data['s'] = $this->N_model->editdata();
        $this->load->view('Neditview',$data);
    }
    public function loadEdit()
    {
        $id = $this->input->post('id');
        $this->load->view('Neditview');
    }
    public function insertdata()
    {
        $eID = isset($_POST['Id'])?$_POST['Id']:'';

        $arr['Name'] = $_POST['Name'];
        $arr['Gender'] = $_POST['Gender'];
        $arr['Email'] = $_POST['Email'];

        $this->load->Model('N_model');
        $res = $this->N_model->updatedata($arr , $eID);
        if($res){
            header('location:'.base_url()."index.php/Nhome/".$this->index());
        }
    }
    public function delete(){
        $this->load->Model('N_model');
        $id = $this->input->post('Id');
        $this->N_model->deletedata($id);
        $this->index();
    }
}

Did you try to debug using Chrome debugger. You might get the exact error in debugger. I suggest you to try once let me know the error name.

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