我尝试开发一个简单的评论系统。 foreach行$result
包含名为posts的数据库中的变量。
帖子的ID(每个帖子都是列表项)是AUTO_INCREMENT,每个帖子都有唯一的$ id,帖子的作者和评论被提交到一个文件,该文件将三个值插入到名为的数据库中评论。
第二个foreach循环$comments
应该在用户单击弹出按钮时显示注释。这不起作用,因为id <input type="hidden" name="id" id="id" value="<?PHP echo $row['id']; ?>" type="text" />
不会被正确插入(comments.php应该这样做)。
结果是所有评论都显示在每个列表项中,而不是唯一这些评论是例如在列表项2.如何将列表项的$ id插入数据库?
<div data-role="content">
<?php include( "list.php"); ?>
<div data-demo-html="true">
<ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">
<?php foreach ($result as $key=> $row): ?>
<li id="listone" name="listone" data-role="list-divider" role="heading" class="ui-li ui-li-divider ui-bar-b ui-li-has-count ui-first-child">
<?php echo $row[ 'date']; ?>
</li>
<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="a">
<div class="ui-btn-inner ui-li">
<div class="ui-btn-text">
<a class="ui-link-inherit">
<p class="ui-li-aside ui-li-desc"><strong></strong>
<?php echo $row[ 'time']; ?>
</p>
<p class="ui-li-desc"><strong><?php echo $row['title']; ?></strong>
</p>
<p class="ui-li-desc">
<?php echo $row[ 'text']; ?>
</p>
<p class="ui-li-desc"><strong><?php echo $row['town']; ?></strong>
</p>
<a href="#popupcomment" data-rel="popup" data-position-to="window" data-transition="pop">comment</a>
<div data-role="popup" id="popupcomment" data-theme="a" class="ui-corner-all">
<form data-ajax="false" name="login-form" class="login-form" action="./comments.php" method="post" style="padding:20px 40px;">
<div class="content">
<?php include( "showcomments.php"); ?>
<?php foreach ($comments as $keyComment=> $rowComment): ?>
<p class="ui-li-desc"><strong><?php echo $rowComment['username']; ?></strong>
</p>
<p class="ui-li-desc">
<?php echo $rowComment[ 'comment']; ?>
</p>
<?php endforeach; ?>
<input type="hidden" name="id" id="id" value="<?php echo $row['id']; ?>" type="text" />
<input type="hidden" name="autorpost" id="autorpost" value="<?php echo $row['autor']; ?>" type="text" />
<!-- autor des posts -->
<textarea rows="1" name="text" id="text" class="foo"></textarea>
</div>
<div class="footer">
<input type="submit" name="save" value="comment" class="button" data-theme="a" />
</div>
</form>
</div>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- div content -->
</div>
</div>
showcomments.php:
$hostname='localhost';
$user='root';
$password='';
$id = $row['id'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, username, comment, time
FROM comments
WHERE id_post = $id
ORDER BY id DESC";
// oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($com = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$comments = $com->fetchAll();
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
的comments.php:
$hostname='localhost';
$user='root';
$password='';
if(isset($_POST["id"])){
try {
$dbh = new PDO("mysql:host=localhost;dbname=searchfood", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
// prepare your query
$query = 'INSERT INTO comments
(username, autorpost, comment, id_post, time)
VALUES (?, ?, ?, ?, now())';
$stmt = $dbh->prepare($query);
// bind variables
$stmt->execute(array($_COOKIE['username'], $_POST['autorpost'], $_POST['text'], $_POST['id']));
// pull last insert id
$new = $dbh->lastInsertId();
// show success message or redirect, whatever you want
echo "New Record Inserted Successfully";
$message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
header("Location: http://".$_SERVER['HTTP_HOST']."/lendsth/main.php", true, 302);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
答案 0 :(得分:0)
基本上,您需要在数据库中创建一个列,用于存储哪些注释在哪个&#34; id_post&#34; - 理想情况下,您可以为帖子创建表格,并为评论创建表格。那,或者如果你没有将帖子存储在数据库中,那么每个帖子都需要有一个与每个评论一起存储的某种唯一标识符。
您的表格将如下设置: 第一张表 - &gt;存储&#34;帖子&#34;这将有评论
表 - 帖子: post_id,post_content
表 - 评论: comment_id,comment_content,post_id(这是用户在其下发布的帖子的ID - 您可以将其存储在隐藏的post_id输入中,并使用POST将其传递给表单处理。
您的选择将如下所示
"SELECT * FROM comments WHERE post_id = '{your post id}'
我不知道这是否会有所帮助,但可能会引导您走上正确的道路。