【问题标题】:Form resubmission issue PHP SQL表单重新提交问题 PHP SQL
【发布时间】:2014-03-22 14:10:10
【问题描述】:

如果我通过 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>

【问题讨论】:

标签: php html sql forms


【解决方案1】:

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

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

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

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

4.转到 1.

【讨论】:

    【解决方案2】:

    只需使用会话确保表单仅提交一次即可。 这应该有效(我无法测试它)

    <?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>
    

    【讨论】:

    • 感谢您对 Emillio 的评论。我认为我不能使用 session_start(),因为我在表单中有其他 php 语句:/ 你知道解决这个问题的方法吗?
    • 好吧,你可以看看 ob_start() 和 ob_end_flush() - 你可以使用它们,所以在你完成页面渲染之前不会向浏览器发送任何内容(这样你就可以设置会话) .但同样,您需要向现有文件中添加至少一行代码。
    • 另外,您可以通过 javascript 设置 cookie,然后使用 PHP 读取它们。由于您唯一的要求是防止意外提交,因此应该可以。
    【解决方案3】:

    阅读这篇文章,它将解释如何重定向,以便刷新不会重新提交表单。

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

    【讨论】:

    • -1 因为答案中只有一个链接(请添加解释/示例。参考应该是支持信息,而不是唯一信息)。
    • 谢谢cmets..我不太清楚如何实现post/redirect/Get模式..这只是意味着当用户点击提交时它会重定向到另一个表单吗?
    • 它只是意味着重定向到当前url并退出插入db之后。
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2020-06-08
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多