は私の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>
"変数を設定しない最適な解決策のように思えます。"何故なの?それが最も簡単な解決策です。 – duskwuff
私はコーディングの初心者です。設定変数はメモリを膨らませるので避けるべきだと思いました。私はgoto()などの行に沿って何かを考えていたでしょう。 – mechanicarts
@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) –