0

我正在尝试制作一个简单的评论系统。当我运行代码时,我可以发表评论并且评论也会显示出来。但问题是错误消息也显示在评论上方,上面写着

未定义索引:在 (C:\wamp\www\comment.php) 中的注释

索引页

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>commenting system</title>
</head>

<body>
<h3> leave a comment </h3>
    <?php include('comment.php');?>

</body>
    </html>

评论页面

<?php 
mysql_connect('localhost','root','');
mysql_select_db('mydb');
$username= $_POST['username'];
$email= $_POST['email'];
$comment= $_POST['comment'];
$submit=$_POST['submit'];

if(isset($email)&& ($username)&&($comment)&&($submit)) {
    $insert=mysql_query("INSERT INTO comment(username,email,comment) VALUES ('$username','$email','$comment')");

    }else{

    echo 'please enter the required field'; 
    }

 ?>
        <div id="commentbox">
                  <div class="show_comments">
                    <?php 
                    mysql_connect('localhost','root','');
                    mysql_select_db('mydb');
                    $sqlQuery="SELECT username, comment FROM comment";
                    $sql=mysql_query("$sqlQuery") or die(mysql_error());
                    while($row =mysql_fetch_assoc($sql)){
                        echo $row['username'];
                        echo '<br/>';
                        echo $row['comment'];
                        echo '<hr />';

                    }


                    ?>   

                 </div>

                    <form action="comment.php" method="POST">
                        name:<br/>
                        <input type="text" name="username"  placeholder="name"/> <br/>
                        email: <br/>
                        <input type="text" name="email" placeholder="email"/> <br />
                        comment: <br />
                        <textarea  rows="10" cols="40" name="comment" placeholder="your comment"> </textarea>
                    <input type="submit" value="comment" name="submit" /> 
                 </form>

         </div>

PS我是这个很棒的编码世界的新手。我会很感激任何帮助!先感谢您!

4

2 回答 2

0

这是因为您没有检查表单是否已提交。用以下内容包围您的查询和$_POST变量:

if (!empty($_POST)) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    $submit = $_POST['submit'];

    if (isset($email) && ($username) && ($comment) && ($submit)) {
        $insert = mysql_query("INSERT INTO comment(username,email,comment) VALUES ('$username','$email','$comment')");
    } else {
        echo 'please enter the required field';
    }
}
于 2013-03-12T17:34:02.253 回答
0
if(isset($row['comment']) {
    echo $row['comment'];
}

还。在文件的开头,使用

error_reporting(E_ERROR);

或者

error_reporting(0);
于 2013-03-12T17:35:23.300 回答