<html>
<head>
<body>
<form action="default.php" method="POST">
<input type="text" name="hname">
<input type="submit" name="submit" value="Submit">
</form>
<?php
require("connect.php");
$con = mysql_connect("mysql10.000webhost.com","$username","$password");
$pname=$_POST[hname];
mysql_query("INSERT INTO web_members(name) VALUES('$pname')");
$result = mysql_query("SELECT * FROM web_members");
while($row = mysql_fetch_array($result))
{
echo $row['name'];
echo "<br />";
}
?>
基本上它添加并显示变量就好了,但每当我刷新页面时它都会重复这个过程。任何人都可以帮忙吗?
答案 0 :(得分:1)
使用如下代码
<?php
require("connect.php");
$con = mysql_connect("mysql10.000webhost.com","$username","$password");
if(isset($_POST[hname]))
{
$pname=$_POST[hname];
mysql_query("INSERT INTO web_members(name) VALUES('$pname')");
header("location:default.php");
}
else
{
$result = mysql_query("SELECT * FROM web_members");
while($row = mysql_fetch_array($result))
{
echo $row['name'];
echo "<br />";
}
}
?>
希望它会对你有所帮助。感谢。
答案 1 :(得分:0)
您需要使用POST/REDIRECT/GET pattern。它可以防止重新提交表单。