我已经尝试了所有的东西,我点击提交后直接显示的数据显示,但是一旦我刷新页面,它就消失了。如何正确回显已存储在数据库中的数据?这是我目前的代码!
<h1>News Blog</h1>
<form method="POST">
username: <input type="name" name="name" placeholder="name"><br />
<textarea name="body" rows="10" cols="70"></textarea><br/>
<input type="submit">
</form>
<?php
if(isset($_POST['name'], $_POST['body'])){
require'core/connect.php';
//inserting data
$query = dbConnect()->prepare("INSERT INTO blog (name, body) VALUES(?,?)");
$query->bindParam(1, $_POST['name']);
$query->bindParam(2, $_POST['body']);
$query->execute();
//getting data
$stmt = dbConnect()->prepare("SELECT name, body FROM blog WHERE name=:name AND body=:body");
$stmt->execute();
while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
echo $rows['name'], '<br /><hr>';
echo $rows['body'];
}
}
?>
答案 0 :(得分:0)
您错过了为选定查询绑定指定占位符:name
和:body
的参数:
//getting data
$stmt = dbConnect()->prepare("SELECT name, body FROM blog WHERE name=:name AND body=:body");
// Add following two lines
$stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
$stmt->bindParam(':body', $_POST['body'], PDO::PARAM_STR);
$stmt->execute();
while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
echo $rows['name'], '<br /><hr>';
echo $rows['body'];
}