我正在尝试制作一个简单的评论系统。 当我运行代码时,我可以发表评论,评论也会显示出来。但问题是错误消息也显示在评论上方
未定义的索引:评论 (C:\瓦帕\ 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我是这个令人敬畏的编码世界的新手。 我很感激任何帮助!提前谢谢!
答案 0 :(得分: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';
}
}
答案 1 :(得分:0)
if(isset($row['comment']) {
echo $row['comment'];
}
另外。在文件的开头,使用
error_reporting(E_ERROR);
OR
error_reporting(0);