今、私はユーザー登録ページを設定しようとしていますが、電子メールがまだ使用されていないことを確認することに問題があります。私がしなければならなかったことは、電子メールがすでに使用されているかどうかをチェックして確認するために、データベースにクエリを出すことでした。これはかなりまっすぐに見えるので、なぜ私にこのような問題を与えているのか分かりません。電子メールがまだ登録されていないことを確認するPHP Mysql
私はいくつかの記事を読んで、PDOとmysqliでいくつかのアプローチを試みましたが、まだこのスクリプトを適切に機能させるにはまだありません。どんな助けでも大歓迎です。
<?php
session_start();
if(isset($_SESSION['user_id'])){
header("Location: /");
}
require 'database.php';
$message = '';
if(!empty($_POST['email']) && !empty($_POST['password'])&& !empty($_POST['firstname'])&& !empty($_POST['lastname'])&& !empty($_POST['phone'])&& !empty($_POST['address'])&& !empty($_POST['city'])&& !empty($_POST['zip'])):
//check to see if e-mail is already being used
//This method always says that the email is already in use, even if I am entering a new one.
/*
$records = $conn->prepare('SELECT * FROM users WHERE email = :email');
$records->bindParam(':email', $_POST['email']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
if(count($results) > 0){
$message = "Sorry, that E-mail address is already registered to an account.";
}
*/
//this one never says that the email is in use.
/*
$email = $_POST['email'];
$query = mysqli_query($conn, "SELECT * FROM users WHERE email='".$email."'");
if(mysqli_num_rows($query) > 0){
$message = "Sorry, that E-mail address is already registered to an account.";
}
*/
//this was the last method I tried, and it also never says that the email is in use.
try{
$stmt2 = $conn->prepare('SELECT `email` FROM `user` WHERE email = ?');
$stmt2->bindParam(1, $_POST['email']);
$stmt2->execute();
while($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
}
}
catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
if($stmt2->rowCount() > 0){
//echo "The record exists!";
$message = "Sorry, that E-mail address is already registered to an account.";
}
else{
// Enter the new user in the database
$sql = "INSERT INTO users (email, password, firstname, lastname, phone, address, city, zip) VALUES (:email, :password, :firstname, :lastname, :phone, :address, :city, :zip)";
$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(':lastname', $_POST['lastname']);
$stmt->bindParam(':phone', $_POST['phone']);
$stmt->bindParam(':address', $_POST['address']);
$stmt->bindParam(':city', $_POST['city']);
$stmt->bindParam(':zip', $_POST['zip']);
if($stmt->execute()):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must have been an issue creating your account';
endif;
}
endif;
?>
あなたはどのようなエラーを取得していますか? –
こんにちは、私はbindParamは、$ sth-> bindParam( ':カロリー'、$カロリー、PDO :: PARAM_INT)のような3つのパラメータが必要だと思います。最後のものはデータ型です。その結果、動作しなくなる可能性があります。詳細については、http://php.net/manual/en/pdostatement.bindparam.phをチェックしてください。 –
'bindParam( ':email'、$ _POST [" email "]、PDO :: PARAM_STR、100);'あなたの最初の試み。 – Evochrome