我在表中获取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>
控制器代码
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
答案 0 :(得分:1)
<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>
控制器:
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 ('') ].
...
...
...
}
答案 1 :(得分:0)
在您的代码中进行此更改以使其正常工作
<input type="checkbox" name="record_ids[]" value="$a->id">
在您的控制器中访问它们
<强>控制器强>
function delete(){
$record_ids = $this->input->post('record_ids');
$result = $this->User_model->deletebyid($record_ids);
if($result==true)
return true;
}
<强>模型强>
----更新处理单删除----
function deletebyid($ids){
if(is_array($ids))
$this->db->where_in('id', $ids);
else
$this->db->where('id',$ids);
$this->db->delete('user_table');
if ($this->db->affected_rows() > 0) {
return true;
}
else
return false;
}
答案 2 :(得分:0)
尝试这样的事情。您将记录ID放在链接中 型号:
function delete_row() //model named displaym
{
$this->db->where('id', $this->uri->segment(3)); //this will be the table row ID
$this->db->delete('table');//table name
}
控制器
public function delete()
{
$this->is_logged_in();
$this->displaym->delete_row();
redirect("page");
}
链接到删除控制器时
anchor("updates/delete/" . $row->id, //send table row ID to the model as the 3rd segement