私はすべてのユーザ名(メールアドレス)を一意にする必要がある登録フォームを持っています。データベースエントリが一意でなければならないようにしましたしかし、ユーザーが取得するエラーは、私が別のメッセージに変更できる一般的なものです($ message参照)が、エラー・サーバー側でアカウントが作成されていないかどうかは分かりません。重複した電子メールアドレス。私が取得するために苦労してきた何ユーザ名を入力する方法
$message = 'Sorry there must have been an issue creating your account';
は次のように述べているカスタムエラーを持つ方法である:「申し訳ありませんが、このメールはすでに使用されている」ユーザ名が既に使用されているとき。
以下は私の登録フォームのコードです)(私はこれを作るdidntは):
<?php
session_start();
if(isset($_SESSION['user_id'])){
header("Location: restricted.php");
}
require 'database.php';
$message = '';
if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['firstname']) && !empty($_POST['surname'])):
// Enter the new user in the database
$sql = "INSERT INTO users (email, password, firstname, surname) VALUES (:email, :password, :firstname, :surname)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':surname', $_POST['surname']);
if($stmt->execute()):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must have been an issue creating your account';
endif;
endif;
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<?php include '../header.php'; ?>
</head>
<body>
<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>
<h1>Register</h1>
<span>or <a href="login.php">login here</a></span>
<form action="register.php" method="POST">
<input type="text" placeholder="Enter your email" name="email">
<input type="password" placeholder="and password" name="password">
<input type="password" placeholder="confirm password" name="confirm_password">
<input type="text" placeholder="Enter your first name" name="firstname">
<input type="text" placeholder="Enter your surname" name="surname">
<input type="submit">
</form>
</body>
</html>
"一般的なエラー"を挿入して頼るよりも、その電子メールを持つユーザーがいるかどうかを確認してください。 –
メールが一意の場合は、メールを選択して、データベースに挿入されているかどうかを確認してから挿入してください。 –