我创建此表单供人们留下反馈,但是当您留下反馈并刷新/重新加载页面时,它会再次发布它。有没有办法让它说出像#34;会话已过期"?这是代码:
<form action="feedback.php" method="POST">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="name">Name *</label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comment">Message *</label>
</td>
<td valign="top">
<textarea name="comment" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" name="add" value="Add FeedBack">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
if($name){
if($email){
if($comment){
mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
}
else
echo "You haven't entered any comment!";
}
else
echo "You haven't entered an email address!";
}
else
echo "You haven't entered your name!";
}
?>
<?php
$run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
$numrows = mysql_num_rows($run);
if($numrows > 0){
while($row = mysql_fetch_assoc($run)){
$dbname = $row['name'];
$dbcomment = $row['comment'];
echo "Commented By $dbname<br>$dbcomment<br><br>";
}
}
else
echo "<br>There are no feedbacks made";
?>
答案 0 :(得分:1)
<?php
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$sql = mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','".$name."','".$email."','".$comment."')");
if($sql == ''){
echo "You haven't entered any comment! or You haven't entered an email address! or You haven't entered your name!";
}
}
$run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
$numrows = mysql_num_rows($run);
if($numrows > 0){
while($row = mysql_fetch_array($run)){
$dbname = $row['name'];
$dbcomment = $row['comment'];
echo "Commented By ".$dbname."<br>".$dbcomment."<br><br>";
}
}
else
echo "<br>There are no feedbacks made";
?>
答案 1 :(得分:1)
您可以根据halfer方法执行此操作:
位于页面顶部:
<?php
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
if($name){
if($email){
if($comment){
mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
//redirect
header("Location: mypage.php");
}
else
$msg = "You haven't entered any comment!";
}
else
$msg = "You haven't entered an email address!";
}
else
$msg = "You haven't entered your name!";
}
?>
在您的HTML代码中
<?php
if(isset($msg)) {echo $msg;}
$run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
$numrows = mysql_num_rows($run);
if($numrows > 0){
while($row = mysql_fetch_assoc($run)){
$dbname = $row['name'];
$dbcomment = $row['comment'];
echo "Commented By $dbname<br>$dbcomment<br><br>";
}
}
else
echo "<br>There are no feedbacks made";
?>
请注意,如果!$name
或!$email
或!$comment
,您无法重定向,这意味着:浏览器会在刷新页面时要求重新提交表单,这是我发生的事件形容丑陋。如果还有任何插入,您可以重定向,以避免这种情况:
<?php
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
if($name){
if($email){
if($comment){
mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
//redirect
header("Location: mypage.php");
}
else
$msg = "You haven't entered any comment!";
}
else
$msg = "You haven't entered an email address!";
}
else
$msg = "You haven't entered your name!";
if(isset($msg)) {header("Location: mypage.php?msg=".$msg);}
}
?>
然后:
<?php
if(isset($_GET['msg'])) {echo $_GET['msg'];}
$run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
$numrows = mysql_num_rows($run);
if($numrows > 0){
while($row = mysql_fetch_assoc($run)){
$dbname = $row['name'];
$dbcomment = $row['comment'];
echo "Commented By $dbname<br>$dbcomment<br><br>";
}
}
else
echo "<br>There are no feedbacks made";
?>