搜索解决方案以获取/发布双倍数据提交

时间:2011-11-05 16:48:11

标签: php web http-method

  

可能重复:
  Avoiding form resubmit in php when pressing f5

我需要有关如何防止双数据提交到mysql数据库的帮助。

这通常发生在提交后,用户点击REFRESH按钮。

每当我使用html web表单发送数据来处理它们在服务器端时,我最好省略一个表单的action属性,解释该文件将由自己处​​理并从第一行开始我放置服务器端php脚本。在发送数据之前,我严格检查是否所有字段都填写正确,然后将该数据发送到将来的处理。使用php脚本我将检查被点击的Web表单按钮是否为空,然后通过已建立的连接到mysql执行操作查询。 使用头函数重定向到自身是防止双数据提交的最佳方法???

Haven你有什么想法吗?

此外,我还会包含代码

<?php
    include_once '../config.php';
    include_once '../functions.php';


    if (isset($_POST['bDep'])){
        $flt=$_POST['flt'];
        $num=$_POST['fltNum'];
        $acr=$_POST['acr'];
        $city=$_POST['city'];
        $fTime=$_POST['fTime'];
        $bReg=$_POST['bReg'];
        $eReg=$_POST['eReg'];
        $acomp=$_POST['aircomp'];
        $gate=$_POST['gate'];
        $dte=$_POST['data'];

        $tm=explode(":",$fTime);
        if ($tm[0]<3) $p=1;
        else $p=0;

        $otmena=$_POST['cancel'];
        if ($otmena==1) $stat=4;
        else $stat=7;



        $sql="INSERT INTO fltdep(FID, FLT, NUM, DEP, TO_FROM, IN_OUT, dte, Stat, BReg, EReg, Gate, AType,  ALn, Cir, Hide ) VALUES (0, '$flt', '$num','$fTime', $city,1, '$dte',$stat,'$bReg','$eReg','$gate',$acr,$acomp,$p,0)";
        if (mysql_query($sql)){
            echo "<p style='text-color:red;text-align:center;'>Успешно добавлена запись № </p>";
        }else {
            echo "<p style='text-color:red;text-align:center;'>Ощибка при добавлении к базе</p>";

        }
    unset($_POST['bDep']);
    unset($_POST['flt']);
    }


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Заполнение вылетов</title>
        <link rel="stylesheet" href="../css/validationEngine.jquery.css" type="text/css"/>
        <link rel="stylesheet" href="../css/template.css" type="text/css"/>
        <link rel="stylesheet" href="../themes/base/jquery.ui.all.css"  type="text/css">
        <link rel="stylesheet" href="../themes/base/flts.css"  type="text/css">
        <link rel="stylesheet" href="../themes/base/tipsy.css" type="text/css" />
        <link rel="stylesheet" href="../themes/base/tipsy-main.css" type="text/css" />
        <link href="../themes/base/table.css" rel="stylesheet" type="text/css" />

        <script src="../js/jquery-1.6.min.js" type="text/javascript"></script>
        <script src="../js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
        <script src="../js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
        <script src="../js/jquery.maskedinput-1.3.js" type="text/javascript"></script>

        <script src="../ui/jquery.ui.core.js"></script>
        <script src="../ui/jquery.ui.widget.js"></script>
        <script src="../ui/jquery.ui.button.js"></script>
        <script src="../ui/jquery.ui.tabs.js"></script>
        <script src="../ui/jquery.ui.datepicker.js"></script>
        <script type="text/javascript" language="javascript">

            jQuery(document).ready(function(){
                // binds form submission and fields to the validation engine
                $("#data").datepicker({ numberOfMonths: 1, dateFormat: 'yy-mm-dd' });                               
                jQuery("#Departures").validationEngine();
            });
            $(function(){
                $('#fTime').mask('99:99',{placeholder:"_"});                
                $('#bReg').mask('99:99',{placeholder: "_"});                
                $('#eReg').mask('99:99',{placeholder: "_"});                
            });
            function SetAline()
            {
                var fl=new String($('#flt').val());
                if (fl.substr(0,2)=='TD' || fl.substr(0,2)=='td' )      { $('#aircomp').val(1); $('#gate').val(2);}
                else if (fl.substr(0,2)=='TJ' || fl.substr(0,2)=='tj' ) { $('#aircomp').val(1); $('#gate').val(1);}
                else if (fl.substr(0,2)=='SM' || fl.substr(0,2)=='sm' ) { $('#aircomp').val(2); $('#gate').val(1);}
                else if (fl.substr(0,2)=='SB' || fl.substr(0,2)=='sb' ) { $('#aircomp').val(3); $('#gate').val(1);}
                else if (fl.substr(0,2)=='OR' || fl.substr(0,2)=='or' ) { $('#aircomp').val(4); $('#gate').val(1);}
                else if (fl.substr(0,2)=='TA' || fl.substr(0,2)=='ta' ) { $('#aircomp').val(5); $('#gate').val(1);}
                else if (fl.substr(0,2)=='SV' || fl.substr(0,2)=='sv' ) { $('#aircomp').val(6); $('#gate').val(1);}
                else if (fl.substr(0,2)=='UT' || fl.substr(0,2)=='ut' ) { $('#aircomp').val(7); $('#gate').val(1);}
                else if (fl.substr(0,2)=='SD' || fl.substr(0,2)=='sd' ) { $('#aircomp').val(8); $('#gate').val(1);}
                else if (fl.substr(0,2)=='YA' || fl.substr(0,2)=='ya' ) { $('#aircomp').val(9); $('#gate').val(1);}
                else if (fl.substr(0,2)=='CS' || fl.substr(0,2)=='cs' ) { $('#aircomp').val(10);$('#gate').val(1);}
                else if (fl.substr(0,2)=='D9' || fl.substr(0,2)=='d9' ) { $('#aircomp').val(11);$('#gate').val(1);}
            }
            function SetRegTime()
            {
                var depTime=$('#fTime').val();  

                var fl=new String($('#flt').val());
                var start,end;
                if (fl.substr(0,2)=='TD' || fl.substr(0,2)=='td'){
                    start='01:30';
                    end='00:20';
                }else {
                    start='03:00';  
                    end='00:40';
                }

                $('#bReg').val(TimeDiff(depTime,start));    
                $('#eReg').val(TimeDiff(depTime,end));  
            }
            function TimeDiff(a,b)
            {

                var first = a.split(":")
                var second = b.split(":")

                    var xx;
                    var yy;

                    if(parseInt(first[0]) < parseInt(second[0])){          

                        if(parseInt(first[1]) < parseInt(second[1])){

                            yy = parseInt(first[1]) + 60 - parseInt(second[1]);
                            xx = parseInt(first[0]) + 24 - 1 - parseInt(second[0])

                        }else{
                          yy = parseInt(first[1]) - parseInt(second[1]);
                          xx = parseInt(first[0]) + 24 - parseInt(second[0])
                        }



                    }else if(parseInt(first[0]) == parseInt(second[0])){

                      if(parseInt(first[1]) < parseInt(second[1])){

                            yy = parseInt(first[1]) + 60 - parseInt(second[1]);
                            xx = parseInt(first[0]) + 24 - 1 - parseInt(second[0])

                        }else{
                          yy = parseInt(first[1]) - parseInt(second[1]);
                          xx = parseInt(first[0]) - parseInt(second[0])
                        }

                    }else{


                      if(parseInt(first[1]) < parseInt(second[1])){

                            yy = parseInt(first[1]) + 60 - parseInt(second[1]);
                            xx = parseInt(first[0]) - 1 - parseInt(second[0])

                        }else{
                          yy = parseInt(first[1]) - parseInt(second[1]);
                          xx = parseInt(first[0]) - parseInt(second[0])
                        }

                    }
                if(xx < 10)
                    xx = "0" + xx
                if(yy < 10)
                    yy = "0" + yy
                return (xx + ":" + yy);  
            }
        </script>
    </head>
    <body>
        <form id="Departures" class="formular" method="post" action="">
            <fieldset>
                <legend>
                ЗАПОЛНЕНИЕ ВЫЛЕТОВ <a href="FillArr.php"><i>Прилет</i></a>

                </legend>
                <label> 
                    <span>Дата : </span>
                    <input class="validate[required] text-input" type="text" name="data" id="data" maxlength="12" value="<? (isset($_POST['data']))?($_POST['data']): date('Y-m-d') ;?>" /> 
                </label>
                <label>
                    <span>Рейс : </span>
                    <input value="" class="validate[required] text-input" type="text" name="flt" id="flt" maxlength="5" onkeyup="SetAline();"/>
                </label>
                <label>
                   <label>
                    <span>№ рейса : </span>
                    <input value="" class="validate[required] text-input" type="text" name="fltNum" id="fltNum" maxlength="5"/>
                </label>
                <label>
                    <span>Тип ВС :</span>
                    <select name="acr" id="acr" class="validate[required]">
                    <option value="">Выберите</option>

                    <?php
                        $AC= & getAC();
                        while ($rowAC=mysql_fetch_array($AC)){
                            $i++;
                                echo "<option value='".$rowAC['acode']."'>".$rowAC['name_ru']."</option>";
                        }
                    ?>
                    </select>
                </label>
                <label>
                    <span>А/П прибт :</span>
                    <select name="city" id="city" class="validate[required]">
                        <option value="">Выберите</option>
                        <?php
                            $city= & getCity();
                            while ($rowCi=mysql_fetch_array($city)){
                                $i++;
                                    echo "<option value='".$rowCi['code']."'>".$rowCi['abbr_ru']."</option>";
                            }
                        ?>
                 </select>
                </label>
                <label>
                   <label>
                    <span>Время отправления : </span>
                    <input value="" class="validate[required] text-input" type="text" name="fTime" id="fTime" maxlength="5" onBlur="SetRegTime();"/>
                </label>
                <label>
                   <label>
                    <span>Начало регистрации : </span>
                    <input value="" class="validate[required] text-input" type="text" name="bReg" id="bReg" maxlength="5" readonly="readonly" />
                </label>
                <label>
                   <label>
                    <span>Конец регистрации : </span>
                    <input value="" class="validate[required] text-input" type="text" name="eReg" id="eReg" maxlength="5"  readonly="readonly"/>
                </label>
                <label>
                    <span>Авиакомпания :</span>
                    <select name="aircomp" id="aircomp" class="validate[required]">
                        <option value="">Выберите авиакомпанию</option>
                        <option value="option1">Таджик Эйр</option>
                       <?php
                            $Aline= & getAComp();
                            while ($rowAline=mysql_fetch_array($Aline)){
                                $i++;
                                    echo "<option value='".$rowAline['acode']."'>".$rowAline['name_ru']."</option>";
                            }
                        ?>
                    </select>
                </label>    
             <label>
                    <span>Терминал :</span>
                    <select name="gate" id="gate" class="validate[required]">
                        <option value="">Выберите</option>
                        <option value="1">1</option>
                        <option value="2">2</option>

                    </select>
                </label> 
                <label> 
                <span>Отмена</span>
                    <input class="checkbox" type="checkbox" name="cancel" id="cancel" value="1"/> 
                </label>
            </fieldset>



            <input name="bDep" class="submit" type="submit" value="Заполнить"/><hr/>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:-2)

你的方法(帖子后重定向)是必要的,但不够。

有些用户按两次链接(双击)。在这种情况下,您的代码有时可能会插入两次相同的记录。您需要编写一个JavaScript,以便在首次单击后禁用提交按钮。