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

时间:2017-07-21 13:53:56

标签: php database codeigniter crud

我是代码点火器的新手,我在rows中更新和删除database时遇到了未知问题。我的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();
    }
}

我的模型代码是:

<?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; }
    }
}

4 个答案:

答案 0 :(得分:0)

更改

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

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

答案 1 :(得分:0)

将您的代码简化为:

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

答案 2 :(得分:0)

用此代码替换您的控制器。因为我认为你将数据作为post方法传递所以你应该在检索数据时使用post。

<?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();
    }
}

答案 3 :(得分:0)

您是否尝试使用Chrome调试器进行调试?您可能会在调试器中得到确切的错误。我建议你试一次让我知道错误名称。