Saya berlatih PHP dan pembuatan database dan ingin mengubah pesan saya berdasarkan kesalahan dari input. Saya tidak tahu bagaimana cara mengembalikan pesan yang diubah dan akan sangat menghargai bantuan yang diberikan.

Ini adalah halaman pendaftaran saya

<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>

Ini adalah halaman pemrosesan saya

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();
    }
}

Saya tidak yakin di mana harus meletakkan variabel pesan perubahan di sini karena saya tidak bisa membuatnya bekerja di pernyataan if.

0
gwolf07 6 Juli 2020, 23:48

1 menjawab

Jawaban Terbaik

Anda memberikan pesan kesalahan sebagai parameter url, sehingga Anda dapat mengaksesnya dengan php $_GET

<h3>
    <?php               
        echo $_GET['error'];     
    ?> 
<h3>
0
Marco somefox 6 Juli 2020, 20:54