简单评论框php和mysql没有';行不通

简单评论框php和mysql没有';行不通,php,mysql,Php,Mysql,此评论框应将评论发送到我的数据库,然后在评论框下显示,但在我提交评论时不会发生任何事情。它只是显示在数据库中。多谢各位 <?php require ('connects.php'); $comment=$_POST['comment']; $submit=$_POST ['submit']; if ($submit) { $insert=mysql_query ("INSERT INTO comment (comment) VALUES ('$comment')" ) ; }

此评论框应将评论发送到我的数据库,然后在评论框下显示,但在我提交评论时不会发生任何事情。它只是显示在数据库中。多谢各位

<?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>

您没有运行查询。您刚刚构造了SQL并将其作为字符串保留。此外,它是按订单,而不是按订单:

<?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"] ;


} 

?>

让我试试看:)


如果这是一个教程,我告诉你这是一个糟糕的教程。不转义get或post的查询意味着sql注入。Mysql接口已弃用,请使用mysqli或PDO接口。此外,不检查查询结果。它可能会失败。您的查询应该选择
id
以及
comment
,因为您稍后会要求它。@Andrew甚至没有注意到这一点。固定的。
<?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>