Php 无法删除CodeIgniter中的记录

Php 无法删除CodeIgniter中的记录,php,codeigniter,Php,Codeigniter,home.php 这是我的控制器,我通过Id获得正确的URL,但无法删除记录 <?php class home extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('select'); $this->load->helper(array('form','url','htm

home.php 这是我的控制器,我通过Id获得正确的URL,但无法删除记录

<?php
class home extends CI_Controller
{       
    function __construct(){
        parent::__construct();
        $this->load->model('select');
        $this->load->helper(array('form','url','html'));
        $this->load->library(array('form_validation','session'));
    }

  public function index(){  
     //load the database  
     //$this->load->database();  
     //load the model  
     $this->load->model('select');  
     //load the method of model  
     $data['h']=$this->select->select();  
     //return the data in view  
     $this->load->view('fetch_view', $data);  
  }   

  function records(){
      $ytl['dta']=$this->users->datas();
       //$this->load->view('info',$ytl);
       //echo "<pre>controller";print_r($ytl);
  }

  function primezone(){ 
       $ytl['dta']=$this->users->datas();
    echo "<pre>controller";print_r($ytl);
  }

  function form($id=null){
    //die($id);
    if($this->input->post())
    {
        //echo"<pre>";print_r( $this->input->post());die;
        $this->form_validation->set_rules('fname','First Name','required|min_length[5]');
        $this->form_validation->set_rules('lname','Last Name','required|callback_check_picture[lname]');
        //$this->form_validation->set_rules('email','Email','required');
        $this->form_validation->set_rules('mobile','Mobile Number','required|callback_valid_phone_number[mobile]');
        $this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>'); 
        if($this->form_validation->run()==True)
        {
            $data=[
            'fname'=>$this->input->post('fname'),
            'lname'=>$this->input->post('lname'),
            'email'=>$this->input->post('email'),
            'mobile'=>$this->input->post('mobile'),
            'message'=>$this->input->post('message'),
            ];
            if(empty($id))
            {
              $ytl=$this->select->insertdata($data);

            } else {
                $ytl=$this->select->updatedata($data,$id);
            }
          if($ytl){
              $this->session->set_flashdata('message', 'Successfully Added.');
          redirect('home');
          }
        } else {
                //$this->load->view('form');
        }
    }
    $ytl['dta']=$this->select->getDataById($id);
    //echo "<pre>";print_r($ytl);die;
    $this->load->view('form',$ytl); 
  }

  public function check_picture($a){
    if(!is_numeric($a))
    {
       return true;
    }else{
       $this->form_validation->set_message('check_picture', 'Please enter only char value');
       return False;
    }
  }

  function valid_phone_number($value){
  $value = strlen($value);
   //echo $value;die;
    if ($value == 10) {
        return true;
    } else {
        $this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10} 
        return false;
    }
  }

  public function delete(){
    $this->load->model('select');
    $id=$this->input->get('id');
       //echo $id; 
     if($this->select->deleteuser($id))
     {
         $data['h']=$this->select->select();
         $this->load->view('fetch_view', $data);
     } 
  }
}
?>
  public function index($msg = NULL){  
     $this->load->model('select');   
         $data = array(
         'h' => $this->select->select(),
         'msg' => $msg
          );
     $this->load->view('fetch_view', $data);  
  } 
         public function delete()  

          {
         $this->load->model('select');
         $id = $this->uri->segment(3);
         //echo $id;
         $result=  $this->select->deleteuser($id);
         if($result==TRUE)
         {
         $msg = '<script>alert("delete successful!!! ")</script>';
           $this->index($msg);
         } 
    }

您调用了url delete($id)函数,但没有在控制器中传递id。请更改您的delete函数


您调用了url delete($id)函数,但未在控制器中传递id。请更改您的delete函数

我希望能帮助你

我希望能帮助你



回显时您是否获得id?回显时您是否获得id?在您的模型中,最好检查$id是否为整数
if(!is_int($id)){throw new\Exception(“$id在尝试删除某些内容时必须是整数”);}
Sir其工作正常,但在删除我的记录后,它给出了错误,我附上了屏幕截图。那么var_dump($data['h'])(在您选择调用后)向您显示了什么?@kumar错误是什么?在屏幕截图中没有错误,只有2条记录显示。我已经编辑了您的问题,因此找不到您的屏幕截图。您可以再次上传吗?在您的模型中,最好检查$id是否为整数
if(!is_int($id)){throw new\Exception(“$id在尝试删除某些内容时必须是整数”);}
Sir其工作正常,但在删除我的记录后,它给出了错误,我附上了屏幕截图。那么var_dump($data['h'])(在您选择调用后)向您显示了什么?@kumar错误是什么?在屏幕截图中没有错误,只显示了2条记录。我已经编辑了你的问题,所以我找不到你的屏幕截图。你能再次上传吗?先生,它工作正常,但删除记录后,它会显示用户列表的双重条目(fetch_视图)。我更新了我的答案。在屏幕截图中没有错误,只显示了2条记录。这是你的意思。你在数据库中的数据比它多。它正在工作,但在删除记录后,它显示了用户列表的双重条目(fetch_视图),我附上了同行评审的屏幕截图。PendingDelete记录之后我看到了双用户列表,在这个URL上你们可以理解我创建的双用户条目。用户列表名字姓氏电子邮件手机号码消息更新删除(下一行)用户列表名字姓氏电子邮件手机号码消息更新删除其工作但删除后记录其显示用户列表的重复条目(获取视图)。我更新了我的答案。在屏幕截图中没有错误,只显示了2条记录。这是你的意思。你在数据库中的数据比它多。它正在工作,但在删除记录后,它显示了用户列表的双重条目(fetch_视图),我附上了同行评审的屏幕截图。PendingDelete记录之后我看到了双用户列表,在这个URL上你们可以理解我创建的双用户条目。用户列表名字姓氏电子邮件手机号码消息更新删除(下一行)用户列表名字姓氏电子邮件手机号码消息更新删除
<?php   
class select extends CI_model
{
    function __construct(){  
      // Call the Model constructor  
      parent::__construct(); 
      $this->load->database();    
    }

    function insertdata($data){
      if(!empty($data['id'])){      
          $this->db->where('id', $data['id'])->update('student', $data);
          return $data['id'];
      } else {
          unset($data['id']);
          $this->db->insert('student',  $data);
          return $this->db->insert_id();
      }
    }


    public function select(){  
       //data is retrive from this query    
        $query = $this->db->get('student');  
        return $query;  
        //return $query->row();
    } 

    function getDataById($id)
    {
      $this->db->select('*');
      $this->db->where('id',$id);
      $this->db->from('student');
      $query = $this->db->get()->row_array();  
           return $query; 
    }

    function updatedata($data, $id)
    {
      //print_r($data);die;
      $this->db->where('id', $id);
      return $this->db->update('student' ,$data);
    }


    public function deleteuser($id)     
    {  
      //echo $id;
      //die;
      // $this->load->database();
      $this->db->where('id', $id);
      $this->db->delete('student');
      return true;
    }

}    
?>
public function delete($id='')  
{
     $this->load->model('select');
     if(!empty($id))
     {
        $data['deldata']=$this->select->deleteuser($id);
        $data['h']=$this->select->select();               
        $this->load->view('fetch_view', $data);
     } 
}
<body>
 echo $msg;
.....your code....
  <a href="<?php echo base_url() . "index.php/home/delete/" . $row->id; ?>">Delete</a>
  public function index($msg = NULL){  
     $this->load->model('select');   
         $data = array(
         'h' => $this->select->select(),
         'msg' => $msg
          );
     $this->load->view('fetch_view', $data);  
  } 
         public function delete()  

          {
         $this->load->model('select');
         $id = $this->uri->segment(3);
         //echo $id;
         $result=  $this->select->deleteuser($id);
         if($result==TRUE)
         {
         $msg = '<script>alert("delete successful!!! ")</script>';
           $this->index($msg);
         } 
    }
 public function select(){  
   //data is retrive from this query  
    $this->db->select('*');  
    $query = $this->db->get('student');  
    return $query;  
    //return $query->row();
} 

 public function deleteuser($id=NULL)     
    {  

     $this->db->where('id', $id);
     $result= $this->db->delete('student');
     return $result;
    }