2016-05-16 108 views
-1

我想实现一个评论系统,但它返回多少次该帖子的评论存在的帖子。评论系统显示单个帖子的多个帖子(php mysql)

请帮我解决这个问题的代码。
谢谢。

我拥有的样本是这样的:

<?php 

include('connect.php'); 

?> 

<form method="post"> 

Subject<br> 
<input type="text" name="subject"><br><br> 
Message<br> 
<textarea name="text"></textarea> 
<br> 
<input type="submit" name="poster" value="Post"> 

</form> 

<?php 

if (isset($_POST['poster'])) { 
    $subject=$_POST['subject']; 
    $message=$_POST['text']; 

    $post=mysql_query("insert into comment (titles, message) values ('$subject', '$message')"); 
    if ($post) { 
     echo "Data got"; 
    }else{ 
     echo "Failed"; 
     echo mysql_error(); 
    } 
} 

$select=mysql_query("SELECT comment.id, comment.titles, comment.message, replies.id, replies.idno, replies.subject, replies.textfile from comment left JOIN replies ON comment.id=replies.id ;"); 


while ($row=mysql_fetch_array($select)) { 

    echo "<b>".$row['titles']."</b><br>"; 
    echo $row['message']."<br>"; 
    echo "<a href=\"edit.php?id=$row[id]\">Reply</a><br>"; 
    echo "<font color='green'><ul>".$row['textfile']."</ul></font><br><br>"; 

} 

?> 

但它返回:

Result

谢谢。

+0

[小博](http://bobby-tables.com/)说:[你的脚本是在对SQL注入攻击的风险。(http://stackoverflow.com/questions/60174/how-可以-I-防止-SQL注入功能于PHP)。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+0

请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

回答

0

你的代码似乎是错误的这条线:

$select=mysql_query("SELECT comment.id, comment.titles, comment.message, replies.id, replies.idno, replies.subject, replies.textfile from comment left JOIN replies ON comment.id=replies.id ;"); 

上LEFT JOIN,comment.id必须以类似replies.commentid例如查询的一端必须是这样对方回复表字段比较:

ON comment.id=replies.commentid;"); 
+0

然后它返回一个错误数组 –

+0

你能解释两个表中的字段名称(注释和回复)。 –

+0

我有它如下:评论=> ID,标题,消息和答复=> IDNO,主题,文本文件,ID(来自评论)。评论表是获取帖子的人,而回复表是获得评论/帖子回复的人。 id和idno取int(20),titles和subject varchar(255),其余取文本。请帮助,谢谢。 –