而是使用模具错误

时间:2014-03-17 17:01:32

标签: javascript php mysql

我目前正在制作注册页面。它包括密码散列和验证。当我使用" die"它会停止表单并显示错误。我想在同一页面上显示错误。

<?php 

// First we execute our common code to connection to the database and start the session 
require("common.php"); 

$ErrorTest ="";
if(!empty($_POST)) 
{ 
    // Ensure that the user has entered a non-empty username 
    if(empty($_POST['username'])) 
    {
        $ErrorTest = "Please enter a username.";
    }       

    if(empty($_POST['password'])) 
    {
        $ErrorTest = "Please enter a password."; 
    }

    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    {
         $ErrorTest = "Invalid E-Mail Address";
    }   

    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            username = :username 
    "; 


    $query_params = array( 
        ':username' => $_POST['username'] 
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        $ErrorTest = "Failed to run query: " . $ex->getMessage(); 
    } 
    $row = $stmt->fetch(); 

    if($row) 
    {
        $ErrorTest = "This email address is already registered";
    } 

    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            email = :email 
    "; 

    $query_params = array( 
        ':email' => $_POST['email'] 
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        $ErrorTest ="Failed to run query: " . $ex->getMessage(); 
    } 

    $row = $stmt->fetch(); 

    if($row) 
    { 
        $ErrorTest =  "This username is already in use"; 
    } 

    $query = " 
        INSERT INTO users ( 
            username, 
            password, 
            salt, 
            email 
        ) VALUES ( 
            :username, 
            :password, 
            :salt, 
            :email 
        ) 
    "; 
    $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 


    $password = hash('sha256', $_POST['password'] . $salt); 

    { 
        $password = hash('sha256', $password . $salt); 
    } 

    $query_params = array( 
        ':username' => $_POST['username'], 
        ':password' => $password, 
        ':salt' => $salt, 
        ':email' => $_POST['email'] 
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    {  
        $ErrorTest = "Failed to run query: " . $ex->getMessage(); 
    } 

    // This redirects the user back to the login page after they register 
    header("Location: myprofile.php"); 
    $ErrorTest = "Redirecting to admin_login.php"; 
} 

&GT;

die ("please enter a username")我想在同一页面上显示它。我也尝试将它放在一个javascript上但是当我在一个文本框上放错了信息时,它会弹出所有错误并显示查询。

1 个答案:

答案 0 :(得分:2)

我采用的方式是将变量赋值替换为$error

即。

die("Failed to run query: " . $ex->getMessage());

替换为

$error = ("Failed to run query: " . $ex->getMessage());

然后使用条件语句包装注册逻辑,检查是否已设置或是否为空(由您决定)

然后,您可以在表单上使用echo输出此消息。

修改

当您重定向到另一个页面而不是在同一页面上处理时,请将代码调整为

if($ErrorTest != ''){ header("Location: register.php?error=".$ErrorTest); } else{ header("Location: myprofile.php"); }

在register.php页面上,只需检查$ _GET [&#39;错误&#39;]然后输出它。

即使错误,您也始终注册用户,因此可能值得将此代码添加到其他内容中