gpt4 book ai didi

php - 发布系统 PHP/MySQL 有错误?,警告 : Illegal string offset 'description'

转载 作者:行者123 更新时间:2023-11-29 23:06:55 26 4
gpt4 key购买 nike

伙计们,我做了 2 个简单的文本输入,存储在我的数据库中,然后使用 foreach 循环检索,因为我想显示表中的所有内容(所有问题)。使用当前的代码,我遇到了多个错误,希望获得一些帮助。

另外,我不确定如果刷新页面,所有问题是否仍会显示在浏览器中,还是仅在单击提交按钮时显示?如果是这样,请告诉我如何制作,以便即使我刷新页面,所有问题都会显示在浏览器中。谢谢

The Errors are Warning: Illegal string offset 'question' in C:\xampp\htdocs\homepage\home.php on line 42
2


Warning: Illegal string offset 'description' in C:\xampp\htdocs\homepage\home.php on line 43
2


Warning: Illegal string offset 'question' in C:\xampp\htdocs\homepage\home.php on line 42
h


Warning: Illegal string offset 'description' in C:\xampp\htdocs\homepage\home.php on line 43
h


Warning: Illegal string offset 'question' in C:\xampp\htdocs\homepage\home.php on line 42
h


Warning: Illegal string offset 'description' in C:\xampp\htdocs\homepage\home.php on line 43
h

<?php

require_once "connection.php";

if(isset($_POST['submit'])) {


$question = $_POST['question'];
$description = $_POST['description'];

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );

if($conn->connect_error) {

die("connection error: " . $conn->connect_error);
} else {

echo "Submit button connected to database!";
}



$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";

if($conn->query($sql)) {

echo "it worked";
} else {

echo "error: " . $conn->error;

exit();
}


$query = "SELECT * FROM `ask` ";

if( $result = $conn->query($query)) {
$fetch = mysqli_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p> {$ques['question']}</p>";
echo "<p> {$ques['description']}</p>";
}
} else {
echo "failed to fetch array";
}






}


?>


<!DOCTYPE html>
<html>
<head></head>
<body>



<div id="submitQuestion">

<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>

</div>









</body>
</html>

最佳答案

试试这个方法:

$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
} else {
echo "0 results";
}
$conn->close();

在此链接中查看更多帮助: http://www.w3schools.com/php/php_mysql_select.asp

关于php - 发布系统 PHP/MySQL 有错误?,警告 : Illegal string offset 'description' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28261277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com