如何在我的页面中回显错误,以便我可以设置样式?

时间:2017-11-23 18:07:41

标签: php html

在我的 php脚本中,我有一个简单的用户名/电子邮件存在条件,但我想把错误(如果它存在)放在我的中的某处html ,这样我就可以设置它并将其放在我的表单上。 Echo只是把它放在左上角。我怎样才能做到这一点?设置变量似乎是不是的最佳解决方案。

<?php
require('connect.php');
if(isset($_POST["register"])){
    $username = $_POST["username"];
    $password = $_POST["password"];
    $email = $_POST["email"];

    $username = mysqli_real_escape_string($conn, $username);
    $password = mysqli_real_escape_string($conn, $password);
    $email = mysqli_real_escape_string($conn, $email);

    $conflictUserQuery = "SELECT username FROM members WHERE username='$username'";
    $conflictUserResult = mysqli_query($conn, $conflictUserQuery);
    $conflictUserRow = mysqli_fetch_array($conflictUserResult, MYSQLI_ASSOC);
    $conflictMailQuery = "SELECT email FROM members WHERE email='$email'";
    $conflictMailResult = mysqli_query($conn, $conflictMailQuery);
    $conflictMailRow = mysqli_fetch_array($conflictMailResult, MYSQLI_ASSOC);
    if(mysqli_num_rows($conflictMailResult) ==1){
        echo "Could not be registered. Mail exists.";
    }
    elseif(mysqli_num_rows($conflictUserResult) ==1){
        echo "Could not be registered. Username exists.";
    }
    else{
        $registerQuery = mysqli_query($conn, "INSERT INTO members (username, password, email) VALUES ('$username', '$password', '$email')");
        if($registerQuery){
            echo "Thank You! you are now registered.";
        }
    }


}
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/style.css">
        <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
        <title>Blog</title>
    </head>
    <body>
        <div class="flex-enable flex-center">
            <div class="flex-enable flex-center semiOverride flex-center px100">
                <div class="flex-enable flex-center flex-column">
                    <h2 class="big-white-title">Register.</h2>
                    <h3 class="medium-white-title">Be a part of this.</h3>
                <form class="flex-enable flex-center flex-column" method="POST">
                    <input class="form-input-text small-white-title" type="text" name="username" placeholder="Username">
                    <input class="form-input-text small-white-title" type="text" name="password" placeholder="Password">
                    <input class="form-input-text small-white-title" type="text" name="email" placeholder="e-mail">
                    <input class="form-button small-white-title" type="submit" name="register" value="Register">
                </form>
                <h3 class="small-white-subtitle">Or <a id="register" href="">login</a> if you have an account.</h3>
                </div>
            </div>
        </div>
        <div id="attribution">Photo by <a href="https://unsplash.com/@meindrittesauge">Sebastian Kanczok</a></div>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

正如我在评论中所述:

这可能就像为其分配变量一样简单,同时使用内联CSS样式化方法:

$error = "<span style=\"color:red;\">Error message here.</span>";

您还可以使用带有.error类的样式表(可以用于多个实例而不是#id)并将其应用于错误元素并简单地放置{{1} }变量,您希望它出现在哪里,并带有$error

注意:使用isset()非常重要,因为它可以避免可能的未定义变量通知。

您也可以使用三元运算符:

对于类似的东西也很有效,因为你可以为它设置默认消息。

如评论中所述,最好使用预先准备好的声明,强烈建议使用安全密码存储/散列方法。

参考文献: