为什么我的插入函数会运行两次?

时间:2016-10-18 17:37:18

标签: php mysql admin

我有一个创建管理员登录的简单功能。它检查密码是否等于确认的密码。但是当你输入错误的确认密码并且说密码不匹配时会发生什么,当你输入正确的确认密码时,它会通过并创建管理员登录并在mysql表中输入两次...如果我最终只将密码和确认的密码正确输入一次,它将通过并插入表中一次。到底是怎么回事?

我的Jquery:

$('#create_adminForm').submit(function() {

 $(this).on('valid.fndtn.abide', function() {

var str = $(this).serialize();

 $('#createResponse').html('Please wait...'); 

        $.ajax({
            type: "POST",
            url:  "inc/functions.inc.php?action=createAdmin",
            data: str,
            success: function(data){ // if success then generate the div and append the the following

                        if(data == "false confirm") {
                            //if passwords do not match
                    $('#createResponse').html('Passwords do not match!'); 
                    $('.login-box').effect( "shake" ); 
                    }
                    if(data == "created") {
                        //else - the account is created! verification email  has sent
                        $('#createResponse').html('Account created!');

   $('#general_modal').foundation('reveal', 'open');
        $('#insertModalHeader').html('Account Created');
            $('#modalContent').html('Admin Account has been created! Please check your email to activate the account. <br><Br> You will be now redirected to the login page...');

                    }

            },
            error: function(jqXHR, status, error) { //this is to check if there is any error
                alert("status: " + status + " message: " + error);
            }
        }); 



     }); 

   });   

我的php:

if($_GET['action'] == "createAdmin") {

$email = filter_input(INPUT_POST, 'email'); 
$username = filter_input(INPUT_POST, 'username'); 
$password = filter_input(INPUT_POST, 'password'); 
$confirm = filter_input(INPUT_POST, 'confirm_password'); 

$current_date = date("Y-m-d h:i:s a");
$microTime = microtime();


if($password == $confirm) {

$hash_pass = crypt($password .$microTime);

$pass_crypt =  crypt($password); // let the salt be automatically generated

$statement_insertAdmin = $conn->prepare("INSERT INTO admin_login(username, password, email, created_date, hash)
VALUES(:user, :pass, :email, :created, :hash)");
$statement_insertAdmin->execute(array(
"user" =>  $username,
"pass" => $pass_crypt,
"email" => $email,
"created" => $current_date,
"hash" => $hash_pass
));

//send verification email with hash
$to = $email; 
$subject = "Admin Account Created";

$htmlbody = 'Please click the link below to activate your admin account! <br><Br>

    <a href=\'http://www.companyname.com/admin/activate.php?  a=1&h='.$hash.'\'>
  Activate Now </a>  
 ';


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

 // More headers
$headers .= 'From: Company Name<email@email.com>';

mail($to,$subject,$htmlbody,$headers);

echo "created"; 

}

else {
echo 'false confirm';       
}

}

0 个答案:

没有答案