我制作了一个包含照片和描述的数据库(php / mysql)。一切都很好。我可以删除整个记录(4张图片和描述),但不知道如何只删除一张图片,例如3.提前感谢您的帮助
delete.php
<?php
// 2. Perform database query
$id = $photo["id"];
$safe_page_id = mysqli_real_escape_string($connection, $id);
$query = "DELETE FROM photographs ";
$query .= "WHERE id = {$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "product removed.";
redirect_to("list_photos_46.php");
echo "Success!";
} else {
// Failure
$_SESSION["message"] = "Failure.";
redirect_to("list_photos_46.php?page={$id}");
die("Database query failed. " . mysqli_error($connection));
}
?>
list_photos.php - 删除所有图片和说明
<td><a href="delete_photo_2.php?id=<?php echo urlencode($row['id']); ?>"onclick="return confirm('are you sure? ');">{Delete}</a></td>
答案 0 :(得分:2)
$query = "DELETE FROM table WHERE id = {'$id'}";
答案 1 :(得分:2)
据我所知,表格中有6列
ID
图片1
图片2
图片2
图片4
描述
当您删除任何记录Picture1,Picture2,Picture3,Picture4时,所有内容都将被删除。如果要删除任何一张图片,可以将该列设置为NULL。例如picture3
$query = "UPDATE photographs SET Picture3 = NULL WHERE id = {$id} ";
或者在插入图片时,为四张图片插入四张记录。通过这种方式,您可以使用代码中的查询删除它们
答案 2 :(得分:1)
您可能需要使用更新查询而不是删除查询
$query = "UPDATE photographs SET filename ='NONE' where id=1";