简单的评论框php和mysql没有用

时间:2013-05-05 08:10:22

标签: php mysql

这个评论框应该将评论发送到我的数据库,然后在评论框下显示,但是当我提交评论时没有任何反应。它只是出现在数据库中。谢谢。

<?php
require ('connects.php');
$comment=$_POST['comment'];
$submit=$_POST ['submit'];


if ($submit) { $insert=mysql_query ("INSERT INTO comment (comment) VALUES    ('$comment')" ) ;


} 


?>


<html>
<head><title>Comment Box | HelperTuts</title></head>
<body>
<form action="comment-box.php" method="POST">

<label>Comment:  </label><br />
<textarea name="comment" cols="25" rows="7"></textarea><br /><br /><br />
<input type="submit" name="submit" value="Comment" /><br />

</form>
<hr width="1100px" size="5px" />

<?php

$getquery="SELECT comment FROM comment ORDER id DESC " ;
while($rows=mysql_fetch_assoc($getquery))
{

$id=$rows['id'] ;
$comment=$rows['comment'];
echo $comment["comment"] ;


} 

?>


</body>
</html>

2 个答案:

答案 0 :(得分:2)

您没有运行查询。您刚刚构建了SQL并将其保留为字符串。此外,它是ORDER BY,而不是ORDER:

<?php

$getquery = "SELECT id, comment FROM comment ORDER BY id DESC ";
$result = mysql_query($getquery) or trigger_error(mysql_error());
while($rows=mysql_fetch_assoc($result))
{

    $id=$rows['id'] ;
    $comment=$rows['comment'];
    echo $comment["comment"] ;


} 

?>

答案 1 :(得分:1)

让我试一试:)

<?php
$mysqli=Mysqli("127.0.0.1","root","DATABASE_PASSWORD","DATABASE_NAME");

$comment=$_POST['comment'];
$comment=$mysqli->real_escape_string($comment);
$submit=$_POST ['submit'];

if ($submit) {
    $insert=$mysqli->query("INSERT INTO `comment`(`comment`) VALUES('".$comment."')");
} 
?>
<!DOCTYPE html>
<html>
<head><title>Comment Box | HelperTuts</title></head>
<body>
<form action="comment-box.php" method="post">
<label>Comment:  </label><br />
<textarea name="comment" cols="25" rows="7"></textarea><br /><br /><br />
<input type="submit" name="submit" value="Comment" /><br />
</form>
<hr width="1100px" size="5px" />
<?php
$getquery="SELECT `comment` FROM `comment` ORDER BY `id` DESC";
$result=$mysqli->query($getquery);
while($rows=$result->fetch_assoc($getquery)) {
    $id=$rows['id'] ;
    $comment=$rows['comment'];
    echo $comment["comment"] ;
} 
?>
</body>
</html>