我想从数据库中删除一行,具体取决于图像的文件名是什么。可以说我有以下2个表:不删除第二张表中的行
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表中删除该行,其中该行包含与图像表中的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();
您正在使用哪种存储引擎?如果你使用'InnoDB',你可以用'ON DELETE CASCADE'添加一个外键(参见http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html ),这将消除您在应用程序代码中处理此问题的需要。 – ruakh
我不知道它是哪个存储引擎,但是如果我需要使用DELETE CASCADE,你能在答案中告诉我代码应该是什么样子吗? – user1681039
如果你有InnoDB,并且用'ON DELETE CONSTRAINT'添加一个外键,那么你可以删除所有从'Image_Question'删除记录的PHP代码,因为一旦你从'Image'删除了一条记录,所有其子记录也会立即被删除。 – ruakh