根据来自不同页面的if语句更改注册页回显消息

rhfm7lfc  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(332)

我正在练习php和数据库创建,并希望根据输入的错误更改我的消息。我不知道如何把修改后的信息传回去,如果有任何帮助,我将不胜感激。
这是我的注册页面

<main>
    <h1>Signup<h1>

    <h3>
        <?php               
            echo $errorMsg;     
        ?> 
    <h3>

    <form action="includes/signup.inc.php" method="post">
    <input type="text" name="uid" placeholder="Username">
    <input type="text" name="mail" placeholder="E-mail">
    <input type="password" name="pwd" placeholder="Password">
    <input type="password" name="pwd_repeat" placeholder="Repeat Password">
    <button type="submit" name="signup-submit">Submit</button>
    <form>
</main>

这是我的处理页面

if(isset($_POST['signup-submit'])){
require 'dbh.inc.php';

$Name = $_POST['uid'];
$Email= $_POST['mail'];
$Password = $_POST['pwd'];
$PasswordRepeat = $_POST['pwd_repeat'];

if(empty($Name) || empty($Email) || empty($Password) || empty($PasswordRepeat)){
    header("Location: ../signup.php?error=emptyfields=1");                          //Check if any field is empty
    exit();
}
else if(!filter_var($Email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $Name)){

    header("Location: ../signup.php?error=invalidamil&uid");                        //Check if username and email is valid input
    exit();
}
else if(!filter_var($Email, FILTER_VALIDATE_EMAIL)){
    header("Location: ../signup.php?error=invalidamil&uid=".$Name);                     //Check if email is valid input
    exit();
}
else if($Password !== $PasswordRepeat){
    header("Location: ../signup.php?error=passwordCheck&uid=".$Name."&mail=".$Email);   // Check if passwords don't match
    exit(); 
}

$sql2 = "SELECT UserName FROM dbo.MainTable WHERE UserName = ?";
$params2 = array($Name, SQLSRV_PARAM_IN);
$stmt2 = sqlsrv_query($conn, $sql2, $params2);

if($stmt2 === false)
{
    die(print_r(sqlsrv_errors(), true));
    exit();
}

$row_count = sqlsrv_num_rows($stmt2);
if($row_count != 0)
{
    $_SESSION['errMsg'] = "Error retrieving username";
    header("location: ../register.php");
    exit();
}
else if($row_count > 0)
{
    $_SESSION['errMsg'] = "Username is already used";
    header("Location: ../signup.php?error=UserNameTaken&uid");                      
    exit();
}

else{

    $sql = "INSERT INTO dbo.MainTable(UserName,Email,UserPassword) 
            VALUES (?,?,?)";    

    $Password = PASSWORD_HASH($_POST['pwd'], PASSWORD_DEFAULT);                 //Password hashing 
    $stmt = sqlsrv_query($conn, $sql,array(@$Name,@$Email,@$Password));

    if($stmt === false){
        die( print_r( sqlsrv_errors(), true));
    }else{
         $_SESSION['errMsg'] = "Registration completed!";
         header("Location: ../signup.php?signup=COMPLETE");                     
         exit();
    }
}

我不确定在哪里放置更改消息变量,因为我无法在if语句中使用它。

kninwzqo

kninwzqo1#

您将错误消息作为url参数提供,因此可以使用php$\u get访问它

<h3>
    <?php               
        echo $_GET['error'];     
    ?> 
<h3>

相关问题