php - 如何删除codeigniter中的特定行?

标签 php codeigniter

我是 codeigniter 的新手。在我的 View 页面中,我在表中显示数据库中的数据,其中有两个用于更新和删除的 anchor 标记。我想通过id从数据库中删除特定行。

我的查看页面是

 <?php foreach($query as $row){ ?>
 <tr>
 <td><?php echo $row->name  ?></td>
  <td><?php echo $row->testi  ?></td>
  <td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
  <td><a href="#ajax-modal2" data-id="delete[]" role="button" class="Delete" data-toggle="modal" onClick="confirm_delete()"><i class="icon-trash"></i></a></td>
  </tr>

<?php } ?>
</table>

我的 Controller 页面是

function delete_row()
{

   $this->load->model('mod1');
   $this->mod1->row_delete();
   redirect($_SERVER['HTTP_REFERER']);  
}

我的模型页面是

 function row_delete()
  {

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

我想通过捕获相应的 id 来删除该行。请不要严厉。谢谢

最佳答案

您在模型中使用了 $id 变量,但您是从任何地方提取它的。您需要将 $id 变量从 Controller 传递到模型。

Controller

让我们通过 row_delete() 方法的参数将 $id 传递给模型。

function delete_row()
{
   $this->load->model('mod1');

   // Pass the $id to the row_delete() method
   $this->mod1->row_delete($id);


   redirect($_SERVER['HTTP_REFERER']);  
}

型号

将 $id 添加到模型方法参数中。

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

现在的问题是您从 Controller 传递了 $id 变量,但它没有在 Controller 中的任何位置声明。

关于php - 如何删除codeigniter中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700582/

相关文章:

PHP 语法错误 “unexpected $end”

php - CodeIgniter 错误、result_array 和 foreach 错误

php - 无法使用 ubuntu 服务器发送谷歌邮件 CodeIgniter

php - Cockpit CMS 安装后不会由 Apache 提供服务(404 错误)

php - 如何打开并运行现有的 Vagrant/Homestead 项目

php - Symfony 表单集合 Twig 渲染

将字符串中的双引号更改为单引号的 PhpStorm 函数

php - codeigniter 默认 Controller url 路由

php - 如何在单行(在<td>中)的html表格中显示具有相同id的多条记录?

php - 无法在 codeigniter 应用程序中将超过 6 种产品添加到购物车