【问题标题】:can't delete record in codeigniter using href无法使用href删除codeigniter中的记录
【发布时间】:2018-05-17 11:41:23
【问题描述】:

我正在尝试使用 href 删除特定记录。但是当我单击按钮时它无法正常工作,它像这样传递 URL

http://localhost/project_x/Organisation/Organisation/%3C?php%20echo%20base_url();?%3EOrganisation/delete_org?id=3

我不知道错误在哪里任何人都可以帮助我。它在 URL 中传递 id 是正确的。

这是我的控制器:

function delete_org() {
   // Pass the $id to the org_delete() method
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}

这是我的重定向组织功能

public function organisation() {

    $data['organisations'] = $this->Org_model->getOrganisations();
    $this->load->view('template/header');
    $this->load->view("organisation", $data);
    $this->load->view('template/footer');
}

这是我的模型:

function org_delete($id) {
    $this->db->where('id', $id);
    $this->db->delete('organisation');
}

这是我的视图按钮:

<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='<?php echo base_url();?>/Organisation/delete_org?id=$org->id'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";                                           
?>

谁能帮我解决我做错的地方。

提前致谢。

【问题讨论】:

  • 在您的delete_org 中,$id 应该来自哪里?它没有定义

标签: php codeigniter url


【解决方案1】:

问题是您在 echo 中再次添加 php 标签。更改您的删除锚标记如下:

 echo "<a href='".base_url()."/Organisation/delete_org?id=$org->id'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";

同样在控制器中定义 $id

function delete_org() {
   // Pass the $id to the org_delete() method
      $id = $_GET['id'];
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}

【讨论】:

    【解决方案2】:

    希望对您有所帮助:

    像这样更改视图按钮:

    <?php
    echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
    echo "<a href='".base_url('Organisation/delete_org?id='.$org->id)."'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
    

    在控制器中:

    function delete_org() { 
          $id = $this->input->get('id');
          $this->Org_model->org_delete($id);
          redirect('Organisation/organisation');
    }
    

    【讨论】:

      【解决方案3】:
      function delete_org($id= NULL) {
      
         // Pass the $id to the org_delete() method
            $id = $this->input->post('id');
            $this->Org_model->org_delete($id);
            redirect('Organisation/organisation');
      }
      hope this helps you
      

      【讨论】:

        【解决方案4】:

        您可以尝试使用 codeigniter 框架通过 data-attribute 删除记录,如下所示。

        html

        <a class="deletedata" data-id="<?php echo $data['id] ?>">delete</a>
        

        js

        $(".deletedata").on("click",function(){
        
          var id=$(this).data('id');
          var baseurl = $("#base_url").val();
        
          $.post(baseurl+"Products/dltProduct/"+id,function(id){
             window.location.reload();
          });
        });
        

        这里我的控制器名称是 Products。你可以定义你的名字和 比定义你的方法名称之后。

        控制器

        public function dltProduct($id) {
          $this->db->where("id",$id);
          $this->db->delete("tbl_name");
        }
        

        【讨论】:

          最近更新 更多