gpt4 book ai didi

php - 通过单击表标题中的删除按钮删除数据

转载 作者:行者123 更新时间:2023-11-29 02:47:56 24 4
gpt4 key购买 nike

我正在获取表中的 10 条记录,现在我有一个名为 DELETE 的表列标题,下面有复选框,现在我想通过单击该 DELETE 按钮删除选中的记录。请告诉我如何在 php codeigniter 中执行此操作

<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Car</td>
<td>Address</td>
<td>Phone Number</td>
<td><a href="<?php echo site_url('User1/delete/'); ?>">Delete</td>
<td>Action </td>
</tr>

<?php foreach($posts as $a){ ?>
<tr>
<td><?php echo $a->id;?></td>
<td><?php echo $a->name;?></td>
<td><?php echo $a->car;?></td>
<td><?php echo $a->address;?></td>
<td><?php echo $a->cell_number;?></td>
<td>
<input type="checkbox" name="id" value="$a->id"><br>
</td>
</tr>
<?php }?>
</table>

Controller 代码

function delete($id){
$result = $this->User_model->deletebyid($id);
if($result==true)
return true;
}

模型代码

function deletebyid($id){
$this->db->where('id', $id);
$this->db->delete('user_table');
if ($this->db->affected_rows() > 0) {
return true;
}else {
return false;
}
}

得到这个错误

A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

DELETE FROM `user_table` WHERE `id` = `Array`

Filename: C:/xampp/htdocs/codeigniter/system/database/DB_driver.php

Line Number: 691

最佳答案

<form method='post' action='<?php echo site_url('User1/delete/'); ?>' >
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Car</td>
<td>Address</td>
<td>Phone Number</td>
<td><input type="submit" name="delete" value="Delete" /></td>
<td>Action </td>
</tr>

<?php foreach($posts as $a){ ?>
<tr>
<td><?php echo $a->id;?></td>
<td><?php echo $a->name;?></td>
<td><?php echo $a->car;?></td>
<td><?php echo $a->address;?></td>
<td><?php echo $a->cell_number;?></td>
<td>
<input type="checkbox" name="r_id[]" value="<?php echo $a->id; ?>"><br>
</td>
</tr>
<?php }?>
</table>
</form>

Controller :

function delete($id){
echo "<pre>";print_r($_POST['r_id']);exit; // you will get all selected check box values. (which are the record ids. You just send this ids to model and delete them using sql IN operator [ex: id IN ('') ].
...
...
...
}

关于php - 通过单击表标题中的删除按钮删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com