簡體   English   中英

登錄/注冊表單上的php標題和消息

[英]php header and messages on login/registration form

我的注冊表單上不斷出現1條以上的消息。 誰能幫助我理解我做錯了什么? 我一直在看它,我不知道我哪里出錯了。

注冊表格

 <?php include_once("config.php");?>
 <?php if( !(isset( $_POST['register']))) { ?>

<!DOCTYPE html>
<html>
    <head>
        <title>Register </title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>
        <header id="head" >
            <p>Please Register</p>
            <p><a href="register.php"><span id="register">Register</span></a></p>
        </header>

    <div id="main-wrapper">
        <div id="register-wrapper">
            <form method="post">
                <ul>
                    <li>
                        <label for="usn">Username : </label>
                        <input type="text" id="usn" maxlength="30" required autofocus name="username" />
                    </li>

                    <li>
                        <label for="passwd">Password : </label>
                        <input type="password" id="passwd" maxlength="30" required name="password" />
                    </li>

                    <li>
                        <label for="conpasswd">Confirm Password : </label>
                        <input type="password" id="conpasswd" maxlength="30" required name="conpassword" />
                    </li>
                    <li class="buttons">
                        <input type="submit" name="register" value="Register" />
                        <input type="button" name="cancel" value="Cancel" onclick="location.href='login.php'" />
                    </li>

                </ul>
            </form>
        </div>
    </div>

</body>
</html>

<?php 
} else {
    $usr = new Users;
    $usr->storeFormValues( $_POST );

    if( $_POST['password'] == $_POST['conpassword'] ) {
        echo $usr->register($_POST);    
    } else {
        echo "Password and Confirm password not match";
        exit;
    }

    $username = ($_POST['username']);
    $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

    $sthandler = $con->prepare("SELECT username FROM users WHERE username = :username");
    $sthandler->execute(array(':username'=>$username));

    if ( $sthandler->rowCount() > 0 ) {
        echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.');
    }else{
        echo  "username is free";
    }
}
?>

注冊功能頁面

 public function register() {
        $correct = false;
            try {
                $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
                $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $sql = "INSERT INTO users(username, password) VALUES(:username, :password)";

                $stmt = $con->prepare( $sql );
                $stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
                $stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
                $stmt->execute();
                return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
            }catch( PDOException $e ) {
                return $e->getMessage();
            }
     }

這是我不斷得到的信息。 我需要1或者其他來顯示。 不是兩個在同一時間。 加號,這是我第一次嘗試注冊j123所以我不確定如何獲取用戶名來顯示用戶名真的已被使用。

Registration Successful

Sorry, the username j123 is already in use.

它很簡單,看起來:

更改:

if ( $sthandler->rowCount() > 0 ) {
        echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.');
    }else{
        echo  "username is free";
    }

為了它:

if ( $sthandler->rowCount() > 0 ) {
        echo ('<br> Sorry, the username '.$_POST['username'].' is already in use.'); exit();
    }else{
        echo  "username is free";
    }

只添加“exit();” 當計數返回超過0時回顯之后。此命令將停止腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM