我正在尝试运行下面的代码,但它给了我这个错误:试图在 c:\xampp\htdocs\. 中获取非对象的属性.\



我正在尝试执行下面的代码,但它给了我一个错误,即"试图在 c 中获取非对象的属性......"。 此代码应提取有关要在索引页面上显示的"图像"和"文本"的信息.php。我已经尝试了各种方法,但无法弄清楚问题出在哪里;顺便说一下,我是PHP的初学者:).如果您请帮助我,我将不胜感激。

<!DOCTYPE html>
<?php
$alert = "";
//if upload button is pressed
if(isset($_POST['upload'])){
//the path to store the uploaded image
$target = "images/".basename($_FILES['image']['name']);
//connect to the database
$conn = new mysqli('localhost', 'imgcms', '', '');
//Get all the submitted data from thye form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO images (image, text) VALUES ('$image', '$text')";

//Move the uploaded image into the folder: images
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
$alert = "Image uploaded successfully";
}else{
$alert = "There was a problem uploading the image";
}
}

?>
<html>
<head>
<title>ImageBlogger</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="content">
<?php
//connect to the database to display image from the database
$conn = new mysqli('localhost', 'imgcms', '', '');
$sql = "SELECT * FROM images";
$result = $conn->query($sql);
if($result->num_rows > 0){
//output data of each row: image and text
while($row = $result->fetch_assoc()){
echo "<div id='img_div'>";
echo "<img src='images/".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
}else{
echo "0 results";
}
$conn->close();
?>
<form action="index.php" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols"40" rows="4" placeholder="Content..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Post the content">
</div>
</form>
</div>
</body>
</html>

将代码复制到编辑器中,看起来像第 47 行;

if($result->num_rows > 0)

在此行之前添加以下内容,看看是否收到错误。

if (!$result) {
echo 'Query Error is: ' . $conn->error;}

最新更新