我在尝试从本地磁盘取消链接文件时出错:
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."')";
我得到"非法字符串偏移'图像'错误,没有文件存在错误。为什么?
您使用了错误的变量。正确的方法是从mysqli获取结果,但您还没有这样做。
当使用fetch_assoc()时(对于mysqli,对于pdo,它只是fetch()),您不必调用num_rows,因为如果没有行,fetch_ansoc将返回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");
}
这是错误的,
$delPath = $query['image'];
更改为
$delPath = $result['image'];
而且这也不会起作用,
alert("File successfully deleted");
redirect("newEntry.php");
如下更新您的代码。
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");
}
}
?>
错误Illegal string offset 'image'
表示您正在使用字符串'image'
作为另一个字符串的偏移量。在这种情况下,因为您试图在错误的变量中找到路径:
$query = "select name,image from image where name = '".$name."' ";
// …
$delPath = $query['image']; // Error: Works for arrays, not strings.