表单重新提交问题PHP SQL

时间:2014-03-22 14:10:11

标签: php html sql forms

如果我通过PHP将新记录插入数据库,然后单击提交按钮,然后每次刷新页面时,它会将最近添加的记录插入到数据库中。我如何阻止这种情况发生?我也不想在提交按钮后重定向到另一个表单。奇怪的是,如果我单击提交按钮两次然后刷新表单它不会将重复项插入数据库。为什么是这样?请帮助:/谢谢

以下是代码:

<div class="insertDiv">
<form method="POST" action="contracts.php">
<?php
if(empty($_POST['ContractDate']) && empty($_POST['ComputerId2']) && empty($_POST['CustomerId'])  && empty($_POST['ContractLevel']))
{
}
else
{
include("dbinfo.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database) or die( "Unable to select database"); 

$contractDate=$_POST['ContractDate'];
$computerID=$_POST['ComputerId'];
$customerID=$_POST['CustomerId'];
$contractLevel=$_POST['ContractLevel'];

$sql="INSERT INTO contract VALUES ('','$contractDate','$computerID', '$customerID', '$contractLevel')";
$result=mysql_query($sql)or die("Insert Error: ".mysql_error());
mysql_close();
}
?>

<div class = "myButton">
Insert
</div>

<p></p>
Enter contract start date:&nbsp
<input type="date" name="ContractDate" size=30 class="input"><br><br>
Enter computerID:&nbsp
<input type="text" name="ComputerId" size=30 class="input"><br><br>
Enter customerID:&nbsp
<input type="text" name="CustomerId" size=30 class="input"><br><br>
Enter contract level:&nbsp
<input type="text" name="ContractLevel" size=30 class="input"><br><br>
<input type="reset" value="Reset" class="button">&nbsp&nbsp&nbsp&nbsp
<input type="submit" value="Submit" class="button">
</form>
</div>

3 个答案:

答案 0 :(得分:0)

检查表格中已有的某些值,如果没有,则将数据插入表格

1.生成一个随机字符串并将其存储在会话中,

2.然后将其作为隐藏值输出到您的表单,

3.检查提交的和存储变量,如果匹配处理您的请求,

4.go to 1。

答案 1 :(得分:0)

通过使用会话,确保表单仅提交一次。 这应该工作(我无法测试)

<?php
// you should start the session before sending any output to the browser
session_start();
if($_SESSION['prevent'] && $_SESSION['prevent'] == $_POST['prevent'])$valid=true;
$_SESSION['prevent']=sha1(mt_rand(0,9999999));

?>
<div class="insertDiv">
<form method="POST" action="contracts.php">
<?php
if(empty($_POST['ContractDate']) && empty($_POST['ComputerId2']) && empty($_POST['CustomerId'])  && empty($_POST['ContractLevel']))
{
}
else
{

if($valid){

include("dbinfo.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database) or die( "Unable to select database"); 

$contractDate=$_POST['ContractDate'];
$computerID=$_POST['ComputerId'];
$customerID=$_POST['CustomerId'];
$contractLevel=$_POST['ContractLevel'];

$sql="INSERT INTO contract VALUES ('','$contractDate','$computerID', '$customerID', '$contractLevel')";
$result=mysql_query($sql)or die("Insert Error: ".mysql_error());
mysql_close();
}
}
?>

<div class = "myButton">
Insert
</div>

<p></p>
Enter contract start date:&nbsp
<input type="date" name="ContractDate" size=30 class="input"><br><br>
Enter computerID:&nbsp
<input type="text" name="ComputerId" size=30 class="input"><br><br>
Enter customerID:&nbsp
<input type="text" name="CustomerId" size=30 class="input"><br><br>
Enter contract level:&nbsp
<input type="text" name="ContractLevel" size=30 class="input"><br><br>
<input type="reset" value="Reset" class="button">&nbsp&nbsp&nbsp&nbsp
<input type="hidden" value="<?php echo $_SESSION['prevent']; ?>">
<input type="submit" value="Submit" class="button">
</form>
</div>

答案 2 :(得分:-1)

阅读本文,它将解释如何重定向,因此刷新不会重新提交表单。

http://en.wikipedia.org/wiki/Post/Redirect/Get