我正在为网站做一个注册页面,我想检查用户是否存在,然后提醒用户。 我尝试了不同的东西,仍然没有工作。 这是我的最新代码:
<?php //script pour vérifier et enregistrer les données
if (isset($_POST['submit']))
{
//on récupère les données entrées
$name = ucfirst($_POST['userName']); //mettre le premier caractère en majuscule
$firstName = ucfirst($_POST['firstName']);
$birthDate = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$languageM = $_POST['langueM']; //langue maternelle
$languageE = $_POST['langueE']; //langue étudiée
$email = $_POST['mailInput'].'@'.$_POST['schools'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
if(isset($name,$firstName,$birthDate,$languageM,$languageE,$email,$password1,$password2))
{
if ($password1===$password2)
{
//hashage du mot de passe
$password1_sha1 = sha1($password1);
$password2_sha1 = sha1($password2);
// taille du nom ou du prénom
if (strlen($name)>25||strlen($firstName)>25)
{
print"<span style=\"color:red;\">Nom ou prénom trop long !</span>";
exit();//plus la peine de continuer
}
else
{
//check password length
if (strlen ($password1)>25 || strlen ($password1)<6)
{
print "<span style=\"color:red;\">Le mot de passe doit etre entre 6 et 25 caractères</span>";
exit();
}
else
{
//connection à la bdd
try
{
$bdd = new PDO('mysql:host=localhost;dbname=bladuo', 'root', '');
$bdd->exec('SET NAMES utf8');//affichage caractères utf-8 dans la bdd
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//enregistrement de l'étudiant dans la bdd
$stmt = $bdd->prepare('INSERT INTO membres(nom,prenom,date_de_naissance,langue_maternelle,langue_etudiee,email,passe)
VALUES(?, ?, ?, ?, ?, ?, ?)');
$success = $stmt->execute(array($name , $firstName , $birthDate, $languageM , $languageE , $email,$password1_sha1));
if($success)
{
header('Location: Index.php');
}else
{
$stmt= $bdd->prepare("SELECT COUNT(*) AS count FROM `membres` WHERE nom=?");
$stmt->execute(array($name));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$username_count = $row["count"];
}
//if username is taken
if ($username_count > 0)
{
print "<span style=\"color:red;\">Cet utilisateur existe déjà!</span>";
}
$stmt = $conn->prepare("SELECT COUNT(*) AS count FROM `user` WHERE email=?");
$stmt->execute(array($email));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$email_count = $row["count"];
}
if ($email_count > 0)
{
print "<span style=\"color:red;\"That email address is already in use<span>";
}
}
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
}
}
}else
{
print "<span style=\"color:red;\">Les mots de passe ne sont pas identiques</span>";
exit();
}
}else
{
print "<span style=\"color:red;\">une des variables nest pas set</span>";
}
}
&GT;
当我尝试注册已存在的用户时,它将保存在数据库中。 我做错了什么?
答案 0 :(得分:0)
我为那些可能遇到同样问题的人更新帖子
<?php //script pour vérifier et enregistrer les données
if (isset($_POST['submit']))
{
//on récupère les données entrées
$name = ucfirst($_POST['userName']); //mettre le premier caractère en majuscule
$firstName = ucfirst($_POST['firstName']);
$birthDate = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$languageM = $_POST['langueM']; //langue maternelle
$languageE = $_POST['langueE']; //langue étudiée
$email = $_POST['mailInput'].'@'.$_POST['schools'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
if(isset($name,$firstName,$birthDate,$languageM,$languageE,$email,$password1,$password2))
{
if ($password1===$password2)
{
//hashage du mot de passe
$password1_sha1 = sha1($password1);
$password2_sha1 = sha1($password2);
// if user exists
$bdd = new PDO('mysql:host=localhost;dbname=bladuo', 'root', '');
$bdd->exec('SET NAMES utf8');//affichage caractères utf-8 dans la bdd
$stmt= $bdd->prepare("SELECT COUNT(*) AS count FROM `membres` WHERE email=?");
$stmt->execute(array($email));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$email_count = $row["count"];
}
if ($email_count > 0)
{
print "<span style=\"color:red;\">Addresse email déjà utilisée, veuillez en choisir une autre!</span>";
}
else
{
//check password length
if (strlen ($password1)>25 || strlen ($password1)<6)
{
print "<span style=\"color:red;\">Le mot de passe doit etre entre 6 et 25 caractères</span>";
exit();
}
else
{
//connection à la bdd
try
{
$bdd = new PDO('mysql:host=localhost;dbname=bladuo', 'root', '');
$bdd->exec('SET NAMES utf8');//affichage caractères utf-8 dans la bdd
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//enregistrement de l'étudiant dans la bdd
$stmt = $bdd->prepare('INSERT INTO membres(nom,prenom,date_de_naissance,langue_maternelle,langue_etudiee,email,passe)
VALUES(?, ?, ?, ?, ?, ?, ?)');
$success = $stmt->execute(array($name , $firstName , $birthDate, $languageM , $languageE , $email,$password1_sha1));
if($success)
{
header('Location: Index.php');
}else
{
echo "INSERT a echouer!!";
exit();
}
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
}
}
}else
{
print "<span style=\"color:red;\">Les mots de passe ne sont pas identiques</span>";
exit();
}
}else
{
print "<span style=\"color:red;\">une des variables nest pas set</span>";
}
}
&GT;