2017-11-23 6 views
1

は私のPHPスクリプトでは、私はシンプルなユーザー名/電子メールが条件が存在する必要がありますが、私はそれをスタイルとそれを上に配置することができるように、どこかで私のHTMLで(それが存在しなければならない)エラーを入れたいです私のフォーム。エコーはちょうどそれを左上に置きます。どうやってやるの?変数を設定すると、ではなく、が最適なソリューションと思われます。あなたはまた、使用することができ私はそれをスタイルすることができるように私のページのエラーをエコーする方法?

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

:インラインCSSスタイリング方法を使用しながら、これは可能性があり

が、それに変数を割り当てるのと同じくらい簡単です:

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

"変数を設定しない最適な解決策のように思えます。"何故なの?それが最も簡単な解決策です。 – duskwuff

+0

私はコーディングの初心者です。設定変数はメモリを膨らませるので避けるべきだと思いました。私はgoto()などの行に沿って何かを考えていたでしょう。 – mechanicarts

+0

@mechanicartsあなたのコードを少し変更することをお勧めします。 **平文のパスワードを保存しないでください!**安全ではないので、PHPはハッシュパスワードにこれらの2つの特別な機能を提供します:['password_hash()'](https://php.net/manual/en/function.password-hash .php)と['password_verify()'](https://php.net/manual/en/function.password-verify.php)それらを使用してください。ハッシュする前に[**パスワードをエスケープしない**](https://stackoverflow.com/q/36628418/5914775)を確認するか、他のクレンジングメカニズムを使用してください。これを行うと、パスワードが変更され、不要な追加のコーディングが発生します。 (1/2) –

答えて

2

は、私がコメントで述べたように.errorクラスのスタイルシート(#idではなく複数のインスタンスで使用できます)をエラー要素に適用し、$error変数を表示したい場所に置き、isset()

注:isset()の使用は、未定義の変数通知の可能性を避けるため、重要です。

はまた、三項演算子を使用することができます。あなたはそれのために、デフォルトのメッセージを設定する可能性があるので、また、非常によくこのような何かのために働く

コメントに記載されているように、準備された文を使用する方が安全で安全なパスワードの保存/ハッシュ方法を使用することを強くお勧めします。

参考文献:

+0

とても助かりました、ありがとう!私は未定義について一定のエラーを出していたので、あなたの 'isset()'を見ました。魅力的な作品! – mechanicarts

+0

@mechanicarts Ahhhhh、そしてそれが私が答えに早く答えを加えたいと思った理由です。その場合は、その基盤をカバーしてください;-)あなたがそれを全部尽くしたことを聞いてうれしく思います!*そしてあなたは一番ですようこそ。 –