简体   繁体   English

无法使用Codeigniter更新或删除数据库中的行

[英]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 . 我是代码点火器的新手,并且在database rows更新和删除时遇到未知的问题。 My code for Controller is : 我对Controller的代码是:

 <?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. 您是否尝试使用Chrome调试器进行调试。 You might get the exact error in debugger. 您可能会在调试器中得到确切的错误。 I suggest you to try once let me know the error name. 我建议您尝试一次,让我知道错误名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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