我想从数据库中删除一行,具体取决于图像的文件名。可以说我下面有两张表:
Image Table:
ImageId ImageFile
01 cat.png
02 dog.png
03 dog_2.png
Image_Question Table:
ImageId SessionId QuestionId
01 AAA 4
02 ABD 1
03 RTD 11
让我们在我的应用程序中说删除文件图像dog_2.png,然后我希望它删除图像表中状态为dog_2.png的行(这很正常)并且能够从Image_Question中删除该行表包含与Image Table中的ImageId和ImageFile名称相关联的相同ImageId的表(这不起作用)。
因此,对于上面的示例,删除后的2个表现在看起来像这样:
Image Table:
ImageId ImageFile
01 cat.png
02 dog.png
Image_Question Table:
ImageId SessionId QuestionId
01 AAA 4
02 ABD 1
但它不会从Image_Question表中删除该行,如何才能删除此行?
下面是从图像表中删除行的完整代码,它包含从Image_Question表中删除行时已设置但尚未完全完成的大部分代码:
$image_file_name = $_GET["imagefilename"];
$img = "ImageFiles/$image_file_name";
echo "$image_file_name was Deleted";
unlink("ImageFiles/$image_file_name");
$imagedeletesql = "DELETE FROM Image WHERE ImageFile = ?";
if (!$delete = $mysqli->prepare($imagedeletesql)) {
// Handle errors with prepare operation here
}
//Don't pass data directly to bind_param; store it in a variable
$delete->bind_param("s",$img);
$delete->execute();
if ($delete->errno) {
// Handle query error here
}
$delete->close();
$imagequestiondeletesql = "DELETE FROM Image_Question WHERE ImageId = ?";
if (!$deleteimagequestion = $mysqli->prepare($imagequestiondeletesql)) {
// Handle errors with prepare operation here
}
// Don't pass data directly to bind_param; store it in a variable
$deleteimagequestion->bind_param("s",....);
$deleteimagequestion->execute();
if ($deleteimagequestion->errno) {
// Handle query error here
}
$deleteimagequestion->close();
答案 0 :(得分:1)
您可以使用JOIN
查询从单个查询中的多个表中删除。我认为这种陈述对你有用:
$sql = "
DELETE img, img_q
FROM Image AS img
LEFT JOIN Image_Question AS img_q
ON img_q.ImageId = img.ImageId
WHERE img.ImageFile = ?";