2016-05-16 109 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),其餘取文本。請幫助,謝謝。 –