unlink() 从服务器删除图像以及 SQL 删除
我目前有一个表格,列出了我的页面数据库中的所有“产品”。每个产品/列表旁边都有一个复选框,因此您可以一次删除多个产品。 我的删除功能与复选框配合良好,从数据库中删除选定的产品,但我似乎无法让后半部分工作,这是需要进入名为“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;
}
I currently have a table listing all of the "products" that are in the database on my page. There is a checkbox next to each product/listing so you can delete multiple products at once.
I have the delete function working well with the checkboxes, deleting the selected products from the database, but I can't seem to get the second half to work, which is the part that needs to go into the server directory named "gallery" and deleting the image that belongs to the products that have been deleted.
When someone clicks the delete button, it runs this code:
//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;
}
}
It won't even do the "echo $file" part which makes me think that that part of the code isnt running at all. Any help?
UPDATED CODE AFTER SWAP:
//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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要交换第一部分和第二部分。您正在尝试选择已删除的数据。
You need to swap first and second part. You're trying to select already deleted data.
您需要在传递字符串时执行 $deleteImgQuery 和 mysql_fetch_array 获取资源作为参数。更改以下行..
You need to execute the $deleteImgQuery and mysql_fetch_array get resource as parameter while you are passing string. Change the following lines..