|
用php5写了个留言的表单,工作正常,但是接F5键刷新时,它又把表单提交一遍,这样留言就重复了.查了一下,好象说用session可以解决,但是弄了半天,还是没弄好,请指教,
下面是我的代码:
<form action="<?php echo $_SERVER['PHP_SELF']?> " method="POST">
<br>留言:<br>
<textarea rows="7" cols="35" name="liuyan_comment"></textarea>
<br>
昵称:<br>
<input type="text" name="liuyan_name">
<input type="submit" value="submit">
</form>
<?php
//$liuyan_name=htmlentities($_REQUEST['liuyan_name']);此句不能显示中文
$liuyan_name=$_REQUEST['liuyan_name'];
$liuyan_comment=$_REQUEST['liuyan_comment'];
$time=date(DATE_ISO8601);
$tmp_date=explode("T",$time);
$tmp_time=explode("+",$tmp_date[1]);
$time=$tmp_date[0]." ".$tmp_time[0];
$mysql_link=@mysql_connect("localhost","liuyan","liuyanadmin");
if(!$mysql_link)
{
echo "Sorry, can not connect to mysql\n";
die();
}
mysql_select_db("liuyandb");
$boolval = $_REQUEST['liuyan_comment']!="" && $_REQUEST['liuyan_name']!="" ;
if($boolval)
{
$liuyan_query="INSERT INTO liuyan20060405 (datetime,name,comment) VALUES('$time','$liuyan_name','$liuyan_comment')";
mysql_query($liuyan_query);
}
//$userLastAction=$liuyan_name;
$query = 'select * from liuyan20060405 order by id desc';
$result=mysql_query($query);
echo "<table>\n";
while($line=mysql_fetch_array($result,MYSQL_ASSOC)){
echo "\t<tr>\n";
foreach($line as $col_value){
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);
mysql_close($mysql_link);
//echo "<br>";
//echo date(DATE_ISO8601);
?> |
|