我在PHP和SQL中创建了一个简单的博客脚本,在需要显示最新帖子的页面上一切正常,但是,我创建了一个页面'comments.php',它将加载所选帖子的评论。 如果你点击帖子1,那么PHP生成的链接就是'comments.php?id = 1'。
这是我在'blog.php'页面上的代码,其中一切正常,发布内容正确显示:
$db = new PDO('mysql:hostname=localhost;dbname=test;charset=utf8', 'root', '');
$req = $db->query('SELECT id, title, content, DATE_FORMAT(creation, \'%d/%m/%Y at %H:%i\') AS datePosted, user FROM blogpost ORDER BY creation DESC LIMIT 0, 5');
while($data = $req->fetch()){
?>
<div class="blogpost-container">
<div class="blogpost-title">
<?php
echo htmlspecialchars($data['title']) . " | <em>Posted on ".$data['datePosted']."</em>";
?>
</div>
<div class="blogpost-content">
<?php
echo nl2br(htmlspecialchars($data['content']));
?>
</div>
<div class="blogpost-user">
<?php
echo "Posted by: ".$data['user'].".";
?>
</div>
<div class="blogpost-comments">
<a href=<?php echo "'comments.php?id=". $data['id']."'"; ?> >
Show comments
</a>
</div>
</div>
<?php
}
$req->closeCursor();
?>
这里,post post正确加载,但是一旦我继续使用comments.php?id = 1,它就没有显示任何内容。这是'comments.php'中的代码:
<?php
$db = new PDO('mysql:hostname=localhost;dbname=test;charset=utf8;', 'root', '');
$req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation, \'%d/%m/%Y at %H:%i), user FROM blogpost WHERE id = ?');
$req->execute(array(
$_GET['id']
));
$data = $req->fetch();
?>
<div class="blogpost-container">
<div class="blogpost-title">
<?php
echo htmlspecialchars($data['title']) . " | <em>Posted on ".$data['datePosted']."</em>";
?>
</div>
<div class="blogpost-content">
<?php
echo nl2br(htmlspecialchars($data['content']));
?>
</div>
<div class="blogpost-user">
<?php
echo "Posted by: ".$data['user'].".";
?>
</div>
</div>
答案 0 :(得分:0)
好的,我找到了我犯了错误的地方。我忘记添加\&#39;在
for /f "tokens=1 delims=." %%g in ('dir /b C:\Users\oRookie\Desktop\batch') do echo %%g >> out.txt