文件夹中的图像已删除,但图像路径在PHP中未更新为null

时间:2018-08-23 06:49:32

标签: php mysql

我创建了一个代码来删除文件夹中的图像,并将image_path更新为数据库中的null。尽管该图像删除了该图像,但是该路径不会更新为null。我花了几个小时才发现自己的错误。但是我不能。任何帮助将不胜感激!

这是我的代码

<?php
//this is were images displayed
$sql    = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
?>
                   <a href="delete.php?delete=<?= $row['id'] ?>" onclick = "return confirm('Are you sure you want to delete?')"><img src="images/template/delete.png" id="AEDbutton">delete</a>

<?php
        echo "<img border=\"0\" src=\"" . $row['image_path4'] . "\" width=\"200\"  height=\"100\">";
        echo "<br>";
    }
}


?>

这是delete.php

<?php
include('config.php');


$sql    = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);

while ($row = $result->fetch_assoc()) {
    $image = $row['image_path4'];
    unlink($image);

}
$sql    = "UPDATE image_path4=null, file_name4=null FROM services WHERE user_name='wendi'";
$result = $con->query($sql);


?>

3 个答案:

答案 0 :(得分:2)

您的查询应如下所示:

$sql = "UPDATE services SET image_path4 = null, file_name4 = null WHERE user_name = 'wendi' ";

答案 1 :(得分:2)

您的UPDATE查询似乎格式错误,the syntax应该像这样:

UPDATE {table} SET {column}={value} WHERE {column2}={value2}

因此,您的情况应该是:

UPDATE services SET image_path4=null, file_name4=null WHERE user_name='wendi'

答案 2 :(得分:1)

在上行链路功能之后,将$image分配给null

$image = null;

并在下面使用更新查询:

$sql = "UPDATE  `services ` SET  `image_path4` = ' ". $image." ', `file_name4` = ' ". $image." ' WHERE `user_name` = 'wendi' ";