多次删除

时间:2013-08-05 08:11:00

标签: database codeigniter checkbox

有人能告诉我如何删除数据库中的多行吗?

例如,在建议之前我有这些 UPDATE ,是否有任何错误?

    <table border='4' cellpadding='5'>

<th>Delete</th>
<th>Update</th>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>

<?php
if(isset($books)) 
{
foreach($books as $book)
    {
?>      <tr>
        <td><input type="checkbox" name="idsToDelete[]" value="<?=$book['book_id']?>" /><td>
        <td><?php echo $book['book_id'] ?></td>
        <td><?php echo anchor("bookstore/delete/1",$book['book_name']) ?></td>
        <td><?php echo $book['book_author'] ?></td>
        <td><?php echo $book['book_year'] ?></td>
        <td><?php echo $book['book_isbn'] ?></td>
        </tr>
<?php
    }
}
else
{
?>

<h2>No records</h2>
<?php
}
?>
</table>

我想要的只是在显示行之前有复选框,当我检查它们并单击提交时,这些行将被删除。

1 个答案:

答案 0 :(得分:1)

试试这个:
视图中

<?php
foreach($books as $each){
?>
<input type="checkbox" name="idsToDelete[]" value="<?=$each['id']?>" />
<?php    
}
?>

现在在控制器中,您将收到一系列ID,因此只需删除ID

<?php
if($this->input->post('idsToDelete')){
    $this->model->delete();
}
?>

模型

<?php
function delete(){
    $idsToDelete    = $this->input->post('idsToDelete');
    $this->db->where_in('id', $idsToDelete)->delete('table');
}
?>