html — я пытаюсь запустить приведенный ниже код, но он выдаёт мне эту ошибку: Попытка получить свойство необъекта в c: \ xampp \ htdocs \ …. \ index.php в строке 47

Я пытаюсь выполнить приведенный ниже код, но он выдает ошибку «пытаюсь получить свойство необъекта в c ….».
Этот код должен получить информацию об «изображениях» и «текстах» для отображения на странице index.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>

0

Решение

Скопировав свой код в редактор, он выглядит как строка 47;

if($result->num_rows > 0)

Перед этой строкой добавьте следующую строку и посмотрите, нет ли ошибки.

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector