我们在 MySQL 数据库的表中显示结果,其中包含复选框。如果用户勾选 5 行并单击删除按钮,我们如何同时删除多行?
问问题
1210 次
3 回答
2
HTML:
<form method="post">
<input type="checkbox" name="id[]" value="123" /> Element 1 <br/>
<input type="checkbox" name="id[]" value="5434" /> Element 2 <br/>
<input type="checkbox" name="id[]" value="24" /> Element 3 <br/>
<input type="checkbox" name="id[]" value="76" /> Element 4 <br/>
<input type="submit" value="delete" />
</form>
PHP:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$ids = array_walk($_POST['id'], 'intval');
mysql_query('DELETE FROM table WHERE id IN (' . implode(',', $ids) . ')');
}
PS我要积分!
于 2011-03-02T16:59:16.227 回答
1
于 2011-03-02T16:59:31.977 回答
1
使用这样的 PDOStatement
$statement = "DELETE FROM table WHERE id = ?";
$conn = new PDO($dns,$user,$pass);
$conn->beginTransaction();
$query = $conn->prepare($statement);
foreach($params as $p){ //$params are your checked id's
$query->bindParam(1,$p);
if($query->execute()){
echo "ok";
}else{
$conn->rollBack();
exit("Error");
}
}
$conn->commit();
于 2011-03-02T17:02:59.613 回答