Home > Backend Development > PHP Tutorial > 为什么刷新一下页面就又重复了上次的留言

为什么刷新一下页面就又重复了上次的留言

WBOY
Release: 2016-06-23 14:11:22
Original
786 people have browsed it


<?phpinclude('conn.php');if($_POST['username']){$sql = "insert into easy values('$_POST[username]','$_POST[title]','$_POST[content]',now())";mysql_query($sql);}?><div align = "center"><table width=500 border="0" cellpadding="5" cellspacing="1" bgcolor="#add3ef">//中间是表单样式,略去<?php   $SQL="SELECT * FROM easy order by posttime desc ";  $query=mysql_query($SQL);  while($row=mysql_fetch_array($query)){?>  <tr bgcolor="#eff3ff">  <td>标题:<?=$row[title]?></td>   <td>用户:<?=$row[username]?></td>  <td>  <?= $row[posttime]?></td>  </tr>  <tr bgColor="#ffffff">  <td>内容:<?= ($row[content])?></td>  </tr><?php  }?></table>
Copy after login


回复讨论(解决方案)

<?phpinclude('conn.php'); if($_POST['username']) //我想是不是这里有问题,但只是刷新页面,并没有传递数据呀
Copy after login

php 不太懂 不过从jsp 的角度出发如果 请求页面数据放在session里面就有那个问题

你略去的部分,恰恰就是最可能的问题原因

提交后header跳转一下。

楼主可否贴出你的提交页面,这样才能定位问题呢。

加一个表单令牌的验证防止生重复提交

我还是全都贴上吧。。


<?phpinclude('conn.php');if($_POST['username']){$sql = "insert into easy values('$_POST[username]','$_POST[title]','$_POST[content]',now())";mysql_query($sql);}?><html><script language = "javascript">function checkMessage(){  if(msgform.username.value == "")  {     alert("用户名不能为空!");     msgform.username.focus();     return fause;  }  if(msgform.title.value =="")  {     alert("标题不能为空!");     msgform.title.focus();     return fause;   }  if(msgform.content.value =="")  {     alert("留言内容不能为空!");     msgform.content.focus();     return fause;   }return true;}</script><body bgcolor = "#FFFFFF" text = "#000000"><h1 align = center><font color = blue>给我留言</font></h1><form name = "msgform" method = "post" action = "input.php" enctype = "multipart/form-data" onsubmit = "return checkMessage()"><table width = "64%" border = "0" cellspacing = "1" cellpadding = "3" bgcolor = "#66CCFF" align = "center"><tr><td width = "48%">用 户:  <input type = "txet" name = "username"></td></tr><tr><td colspan = "2">标 题:<input type = "text" name = "title" size = "60" maxlength = "50"></td></tr><tr><td colspan = "2"><textarea name = "content" rows = "10" cols = "100"></textarea></td></tr><tr><td><div align = "right"><input type = "submit" name = "sub" value = "提交"></div></td><td><input type = "reset" name = "Rewrite" value = "重写"></td></tr></table><div align = "center"><table width=500 border="0" cellpadding="5" cellspacing="1" bgcolor="#add3ef"><?php   $SQL="SELECT * FROM easy order by posttime desc ";  $query=mysql_query($SQL);  while($row=mysql_fetch_array($query)){?>  <tr bgcolor="#eff3ff">  <td>标题:<?=$row[title]?></td>   <td>用户:<?=$row[username]?></td>  <td>  <?= $row[posttime]?></td>  </tr>  <tr bgColor="#ffffff">  <td>内容:<?= ($row[content])?></td>  </tr><?php  }?></table></div></form></body></html>
Copy after login

这个你必须明白一个原理:如果浏览器没有进行跳转,那么你之前的提交动作会被保留,如果这时候你刷新,就会再次出发这个提交动作,虽然不是你点击提交的,但实际上也相当于你提交了

多以提交完以后,你要进行跳转或者其他方式取消掉提交的动作,不要让浏览器保留该动作

<?phpinclude('conn.php'); if($_POST['username']){ $sql = "insert into easy values('$_POST[username]','$_POST[title]','$_POST[content]',now())";mysql_query($sql);  //意思就是在这之后做一个跳转处理或者其他去掉提交动作处理,比如echo '<meta http-equiv="refresh" content="1;url=index.php">'}?>
Copy after login

这个你必须明白一个原理:如果浏览器没有进行跳转,那么你之前的提交动作会被保留,如果这时候你刷新,就会再次出发这个提交动作,虽然不是你点击提交的,但实际上也相当于你提交了

多以提交完以后,你要进行跳转或者其他方式取消掉提交的动作,不要让浏览器保留该动作
+1
提交成功后,跳转一下页面就可以了

if($_POST['username']){ $sql = "insert into easy values('$_POST[username]','$_POST[title]','$_POST[content]',now())";mysql_query($sql);echo echo "<script language=\"javascript\">alert('提交成功');location.href='add.php';</script>"; }
Copy after login

多个打了个echo

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template