我在尝试从本地磁盘取消链接文件时出错:
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");
}
}
?>
其中image是我的图片的路径:
$path = "upload/". $picture_name;
move_uploaded_file($picture_tmp,$path)
$insQuery = "insert into image(name,image,price,description) values('".$name."','".$path."','".$price."','".$desc."')";
我收到“非法字符串偏移'图像'错误,并且没有文件存在错误。 为什么??
答案 0 :(得分:2)
这是错误的,
$delPath = $query['image'];
更改为
$delPath = $result['image'];
而且这也不会工作,
alert("File successfully deleted");
redirect("newEntry.php");
答案 1 :(得分:2)
您使用了错误的变量。正确的是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");
}
答案 2 :(得分:0)
更新您的代码,如下所示。
if(isset($_POST['delete'])){
$name = $_POST['deleteEntryName'];
$query = "select name,image from image where name = '".$name."' ";
$result = $db->query($query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC)
if(mysqli_num_rows($result)>0)
{
$delQuery = "delete from image where name = '".$name."' ";
$delResult = $db->query($delQuery);
$delPath = $row['image'];
unlink($delPath);
echo "<script>alert("File successfully deleted")</script>";
redirect("newEntry.php");
}
else
{
alert("File is not exist");
redirect("newEntry.php");
}
}
?>
答案 3 :(得分:0)
The error Illegal string offset 'image'
means that you are using the string 'image'
as an offset to another string. In this case, because you were trying to find the path in the wrong variable:
$query = "select name,image from image where name = '".$name."' ";
// …
$delPath = $query['image']; // Error: Works for arrays, not strings.