尝试从本地磁盘取消链接我的文件时出现错误:
if(isset($_POST['delete'])){
$name = $_POST['deleteEntryName'];
$query = "select name,image from image where name = '".$name."' ";
$result = $db->query($query);
if(mysqli_num_rows($result)>0)
{
$delQuery = "delete from image where name = '".$name."' ";
$delResult = $db->query($delQuery);
$delPath = $query['image'];
unlink($delPath);
alert("File successfully deleted");
redirect("newEntry.php");
}
else
{
alert("File is not exist");
redirect("newEntry.php");
}
}
?>
其中图像是我的图像的路径:
$path = "upload/". $picture_name;
move_uploaded_file($picture_tmp,$path)
$insQuery = "insert into image(name,image,price,description) values('".$name."','".$path."','".$price."','".$desc."')";
我收到“非法字符串偏移‘图像’错误,并且没有文件存在错误。 为什么??
最佳答案
您使用了错误的变量。正确的应该是从 mysqli 中获取的结果,但您还没有这样做。
当使用 fetch_assoc() 时(对于 mysqli,对于 pdo 它只是 fetch())你不必调用 num_rows 因为如果没有行 fetch_assoc 返回 null。
$name = $_POST['deleteEntryName'];
// WARNING: Possible SQL Injection here!
$query = "select name,image from image where name = '".$name."' ";
// mysqli::query() returns a statement
$selStmt = $db->query($query);
// mysqli::fetch_assoc() returns an array with your columns
if ($selResult = $selStmt->fetch_assoc()) {
$delQuery = "delete from image where name = '".$name."' ";
$delResult = $db->query($delQuery);
$delPath = $selResult['image']; // change here
unlink($delPath);
alert("File successfully deleted");
redirect("newEntry.php");
}
关于php - 使用 unlink() 从数据库中删除带有在 php 中上传的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786719/