我目前有一个表格,列出了我页面上数据库中的所有“产品”。每个产品/列表旁边都有一个复选框,因此您可以一次删除多个产品。我的删除功能与复选框配合得很好,从数据库中删除了选定的产品,但我似乎无法让后半部分工作,这是需要进入名为“gallery”的服务器目录的部分和删除属于已删除产品的图像。
当有人单击删除按钮时,它会运行以下代码:
//if form was submitted
if ($submit && $submit == "Delete") {
//escaping all of them for a MySQL query using array_map
array_map ('mysql_real_escape_string', $allCheckBoxId);
//implode will concatenate array values into a string divided by commas
$ids = implode(",", $allCheckBoxId);
//building query
$deleteQuery = "DELETE FROM products WHERE `id` IN ($ids)";
//running query
mysql_query($deleteQuery);
echo $ids;
//BELOW IS THE PART THAT WILL NOT WORK
//================================================
//building query
$deleteImgQuery = "SELECT * FROM products WHERE `id` IN ($ids)";
while ($deleteImage = mysql_fetch_array($deleteImgQuery)) {
$image_delete_id = $deleteImage['id'];
$image_delete_image = $deleteImage['image'];
$file= "../gallery/" . $image_delete_id . "/" . $image_delete_image;
unlink($file);
echo $file;
}
}
它甚至不会执行“echo $file”部分,这让我认为那部分代码根本没有运行。有什么帮助吗?
交换后更新的代码:
//if form was submitted
if ($submit && $submit == "Delete") {
//escaping all of them for a MySQL query using array_map
array_map ('mysql_real_escape_string', $allCheckBoxId);
//implode will concatenate array values into a string divided by commas
$ids = implode(",", $allCheckBoxId);
//building query
$deleteImgQuery = "SELECT * FROM products WHERE `id` IN ($ids)";
while ($deleteImage = mysql_fetch_array($deleteImgQuery)) {
$image_delete_id = $deleteImage['id'];
$image_delete_image = $deleteImage['image'];
$file= "../gallery/" . $image_delete_id . "/" . $image_delete_image;
unlink($file);
echo $file;
}
//building query
$deleteQuery = "DELETE FROM products WHERE `id` IN ($ids)";
//running query
mysql_query($deleteQuery);
echo $ids;
}