PHP& MySQL删除图像链接问题

时间:2010-04-16 20:45:13

标签: php mysql

如果图像存在,我正在尝试创建删除图像链接,当用户点击删除图像链接时,它应该删除图像。但由于某种原因,这是行不通的,有人可以帮我解决删除图像链接问题吗?谢谢!

这是PHP代码。

if (isset($_POST['delete_image'])) { 
    $img_dir = "../members/" . $user_id . "/images/thumbs/";
    $img_thmb = "../members/" . $user_id . "/images/";

    $image_name = $row['image'];

    if(file_exists($img_dir . $image_name)){
        if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
            $mysqli = mysqli_connect("localhost", "root", "", "sitename");
            $dbc = mysqli_query($mysqli, "DELETE FROM users* WHERE image_id = '.$image_id.' AND user_id = '$user_id'");
        }else{
           echo '<p class="error">Sorry unable to delete image file!</p>';
        }
    }
}

if(isset($_POST['image']) || !empty($image)) {
 echo '<a href="'. $_POST['delete_image'] .'">Delete Image</a>';
}

2 个答案:

答案 0 :(得分:0)

"DELETE FROM users* WHERE image_id = '.$image_id.' AND user_id = '$user_id'"

应该是

"DELETE FROM users WHERE image_id = $image_id AND user_id = $user_id"

这假设$ image_id和$ user_id都是整数。如果它们是字符串,请在它们周围加上单引号。

另外,请仔细检查您的链接:

<a href="'. $_POST['delete_image'] .'">Delete Image</a>

用户是否真的通过POST传递链接?

您的代码容易受到SQL注入攻击。请考虑使用参数化查询。

答案 1 :(得分:0)

“DELETE FROM users WHERE image_id = $ image_id AND user_id = $ user_id”

$ path =“你的图像文件夹路径”; $ image_name =“你的图像名称”;

的unlink($路径“$ IMAGE_NAME。);