0

谁能告诉我有关如何删除数据库中的多行的任何技巧?

比如我有这些,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>

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

4

1 回答 1

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');
}
?>
于 2013-08-05T08:17:44.810 回答