首页 > 解决方案 > PHP页面偶尔会发布多次

问题描述

我有一个将数据发布到 save.php 的注册表单。但有时数据会多次发布。

下面是我的 save.php 代码

<?php
session_start();
//save registration details in my table

include('connect_database.php');
include('my_functions.php');

$_SESSION['newUser'] = '0'; // new user

//POSTED DATA--------------------------
$t_email = $_POST['email'];
$t_psw = $_POST['psw'];
$t_first_name = addslashes($_POST['first_name']);
$_SESSION['lastname'] = $t_last_name = addslashes($_POST['last_name']);
$t_mobile = $_POST['mobile'];
$_SESSION['licNum'] = $t_lic_no = $_POST['lic_no'];
$t_dob = $_POST['dob'];
$t_abn = $_POST['abn'];
$tx_expiry = $_POST['tx_expiry'];
$drv_for = $_POST['driven_for'];
$lng_drv = $_POST['long_driven'];

//referred by
$ref_drLic = $_POST['ref_driLic'];
$ref_drName = $_POST['ref_driName'];

$t_dr_front = get_image('dr_front',$_POST['last_name'].'_dr_front');
$t_dr_bck = get_image('dr_bck',$_POST['last_name'].'_dr_bck');

//if tx required-------
if($_SESSION['ce_cr_tx'] == 1){
    $t_tx_front = get_image('tx_front',$_POST['last_name'].'_tx_front');
    $t_tx_bck = get_image('tx_bck',$_POST['last_name'].'_tx_bck');
}
else{
    $t_tx_front = "";
    $t_tx_bck = "";
}

//store data in logfile
$nwtxt = "Email is - ".$_POST['email'].". Mobile no - ".$_POST['mobile'];
writeFile($nwtxt);

//---------------------------------------
//query to save data in my table 
$ad_sql = "INSERT INTO myTable (email, password, firstname, lastname, mobile, licence, drfront, drbck, txfront, txbck, cnfrm, dob, abnf, texpiry, drifor, driven, reLic, reNname)
    VALUES('".$t_email."','".$t_psw."','".$t_first_name."','".$t_last_name."','".$t_mobile."','".$t_lic_no."','".$t_dr_front."','".$t_dr_bck."','".$t_tx_front."','".$t_tx_bck."','0','".$t_dob."','".$t_abn."','".$tx_expiry."','".$drv_for."','".$lng_drv."','".$ref_drLic."','".$ref_drName."')";

if(!empty($t_email)){
    if($conn->query($ad_sql) == true){
        //echo'Success';
        $lst_id = $conn->insert_id;
        $_SESSION['ls_id'] = $lst_id;
        $_SESSION['s_email'] = $t_email;
        $_SESSION['s_code'] = mt_rand(11111,99999);

        //email code to user--------------------------
        $subjct = "Email Verification Code";
        $usr_msg = "Hi ".$_POST['first_name']." ".$_POST['last_name'].",<br><br>
                    A new account has been requested at 'Portal'
                    using your email address.<br><br>

                    To confirm your new account, please enter this code in the web page:<br>
                    <h3>".$_SESSION['s_code']."</h3><br><br>

                    If you need help, please call us<br><br>

                    Thank you,
                    Administrator";
        sendEmail($t_email, $usr_msg, $subjct); //sends and email
        writeFile('Code is :'.$_SESSION['s_code']); // write a log in file
        //--------------------------------------------      
        //redirect to verify email page----------------------
        header("location: verifyEmail.php");
        exit();


    }
    else{
        echo'Error creating account-  '.$conn->error.'. Please try again.';
        $gbck = "cr=".$_SESSION['ce_cr_id']."&crs=".$_SESSION['ce_cr_nm']."&tx=".$_SESSION['ce_cr_tx']."&erms=Error creating account. Please try again";
        header('location: Enroll.php?'.$gbck);
        exit();
    }
}
else{
    echo'Error creating account. Please try again.';
    $gbck = "cr=".$_SESSION['ce_cr_id']."&crs=".$_SESSION['ce_cr_nm']."&tx=".$_SESSION['ce_cr_tx']."&erms= EMPTY data. Error creating account. Please try again";
    header('location: Enroll.php?'.$gbck);
    exit();
}

?>

我多次检查我的代码,但找不到任何触发它的东西。当有人注册时,页面会持续加载一段时间,我在数据库中收到多个条目,用户收到多个验证电子邮件。

我的代码有问题吗?

标签: phpmysqlpost

解决方案


代码本身看起来不错,但我越来越怀疑它可能是配置问题或者在执行之前发生了什么。如果您正在寻找拼凑修复,我可能会在您附近放置一个条件if(!empty($t_email)),检查 sql 表行是否已经存在,不执行,这将纠正多个请求进入的事实。


推荐阅读