Php codeigniter如何编辑或删除表中的行

Php codeigniter如何编辑或删除表中的行,php,codeigniter,html-table,edit,crud,Php,Codeigniter,Html Table,Edit,Crud,如何获取要编辑或删除的表的相应行的用户id?“我的表”有一个“操作”列,其中包含“编辑”和“删除”按钮。 我的看法是: <table class="table table-striped"> <tr> <td>First Name</td> <td>Last Name</td> <td>Address Name</td> <td>Action

如何获取要编辑或删除的表的相应行的用户id?“我的表”有一个“操作”列,其中包含“编辑”和“删除”按钮。 我的看法是:

 <table class="table table-striped">
   <tr>
     <td>First Name</td>
     <td>Last Name</td>
     <td>Address Name</td>
     <td>Action</td>
   </tr>
  <?php foreach($results as $row): ?>
    <tr>
      <td><?php echo $row->first_name; ?></td>
      <td><?php echo $row->last_name; ?></td>
      <td><?php echo $row->address; ?></td>
      <td><a href="" class="btn btn-info">Edit</a>
      <a href="<?php echo base_url()."main/deleteclient" ?>"                                       
          class="btn btn-danger"
              onclick="return confirm
              ('Are you sure  to Delete?')">Delete</a></td>

                </tr>
                 <?php endforeach; ?>

 </table> 

名字
姓
地址名
行动
更改

<a href="<?php echo base_url()."main/deleteclient" ?>"                                       
          class="btn btn-danger"
          onclick="return confirm
          ('Are you sure  to Delete?')">Delete</a>


然后从PHP脚本中检查变量“id”是否存在于$\u GET中,并处理删除操作

仍在对答案进行格式化。

就像您正在获取其他表值一样,如first name和last name,每行都应该有一个id(如果您提供了主id)。因此,您可以获取每一行的id,并通过URL将其发送到删除函数。
    As like you are getting other table values like first name and last name there should be (if you gave an primary id) an id for each row. So you can get that id for each row and send it to your delete function by URL.

     <?php foreach($results as $row): ?>
           <tr> 

                <td><?php echo $row->first_name; ?></td>
                <td><?php echo $row->last_name; ?></td>
                <td><?php echo $row->address; ?></td>
                <td><a href="" class="btn btn-info">Edit</a>

<a href="<?= base_url();?>main/deleteclient/<?= $row->id;?>"></a>                                      
                  class="btn btn-danger"
                  onclick="return confirm
                  ('Are you sure  to Delete?')">Delete</a></td>

                    </tr>
                     <?php endforeach; ?>
class=“btn btn危险” onclick=“返回确认” (“您确定要删除吗?”)“>删除

在标记中添加$row->id,当您单击该按钮时,这将把行id带到主控制器中的deleteclient函数中。

非常感谢大家。。这就是我删除行的方式。如果有更好的方法,请告诉我

   view: 

    <table class="table table-striped">
      <tr>
       <td>First Name</td>
        <td>Last Name</td>
        <td>Address</td>
         <td>Citizen Number</td>
       <td>Action</td>
   </tr>
    <?php foreach($results as $row): ?>
   <tr> 

   <td><?php echo $row->first_name; ?></td>
   <td><?php echo $row->last_name; ?></td>
   <td><?php echo $row->address; ?></td>
  <td><?php echo $row->citizen_no; ?></td>
  <td><a href="" class="btn btn-info">Edit</a> 
  <a href="<?php echo base_url().
  "main/deleteclient?id=".$row->client_id ?  >" 
  class="btn btn-danger" 
 onclick="return confirm('Are you sure to Delete?')">Delete</a></td>

 </tr>
<?php endforeach; ?>

 </table>  

    controller:
      public function deleteclient(){
     if(isset($_GET['id'])){
        $id=$_GET['id'];
        $this->load->model('members');
        $this->members->row_delete($id);
        redirect('main/viewclient');}
 }
   model:
  public function row_delete(){
        $id=$_GET['id'];
        $this->db->where('client_id', $id);
        $this->db->delete('client');
    }
视图:
名字
姓
地址
公民号
行动
控制器:
公共函数deleteclient(){
如果(isset($\u GET['id'])){
$id=$_GET['id'];
$this->load->model('members');
$this->members->row\u delete($id);
重定向('main/viewclient');}
}
型号:
公共函数行_delete(){
$id=$_GET['id'];
$this->db->where('client_id',$id);
$this->db->delete('client');
}

查询是否也返回ID?您不应该使用链接(GET请求)来删除内容。改为使用表单和POST。您可以发布控制器和模型吗?您的
结果中是否有
id
您是否选择了id字段(主键)??在模型中??如果您选择了该字段,那么它将显示在这里…这是我的模型:公共函数getall(){$query=$This->db->query(“从客户端选择*);return$query->result();}好的,查询似乎很好。。你得到结果了吗??我是说名字、姓氏等?你想通过ajax删除它吗??或者您想将其重定向到页面并捕获Id,然后删除并返回到此列表页面??好的,然后创建一个函数deleteclient并将最后出现的值作为段。。由于您在最后传递了值,因此它类似于deleteclient/2或deleteclient/4。。。。。这些只是行的ID。获取此信息并启动删除查询,并放入重定向代码以再次返回此页面…检查此链接。您将在codeigniter中了解url系统的工作原理。如果您在阅读本文后需要进一步帮助,请告诉我。:)这篇文章很有帮助,但我仍然不知道如何在controller中获取用户id。假设您有一个名为main的控制器,并且在该控制器中有一个函数deleteclient。因此,类似于这个类的Main扩展了CI_控制器{public function deleteclient($id){//mysql查询以删除id.}。当用户单击时,它将触发主控制器deleteclient函数,其中$row->id作为deleteclient($id)函数的参数$id。
   view: 

    <table class="table table-striped">
      <tr>
       <td>First Name</td>
        <td>Last Name</td>
        <td>Address</td>
         <td>Citizen Number</td>
       <td>Action</td>
   </tr>
    <?php foreach($results as $row): ?>
   <tr> 

   <td><?php echo $row->first_name; ?></td>
   <td><?php echo $row->last_name; ?></td>
   <td><?php echo $row->address; ?></td>
  <td><?php echo $row->citizen_no; ?></td>
  <td><a href="" class="btn btn-info">Edit</a> 
  <a href="<?php echo base_url().
  "main/deleteclient?id=".$row->client_id ?  >" 
  class="btn btn-danger" 
 onclick="return confirm('Are you sure to Delete?')">Delete</a></td>

 </tr>
<?php endforeach; ?>

 </table>  

    controller:
      public function deleteclient(){
     if(isset($_GET['id'])){
        $id=$_GET['id'];
        $this->load->model('members');
        $this->members->row_delete($id);
        redirect('main/viewclient');}
 }
   model:
  public function row_delete(){
        $id=$_GET['id'];
        $this->db->where('client_id', $id);
        $this->db->delete('client');
    }