I am currently trying to create a registration form. I have a small problem with the condition that allows me to know if the mail that the user enters, exists or not in my database
Adding the user works fine but when I re-enter the same mail, it is added to the database while it will not have to because the mail already exists
Here is my code:
JS AJAX :
$('#btn_submit').click(function() {
/* Action on the event */
//1. Récupération des données saisie dans les champs du formulaire
var firstname = $('#firstname').val();
var lastname = $('#lastname').val();
var phone = $('#phone').val();
var email = $('#email').val();
// var fullValues = firstname + " - " + lastname + " - " + email + " - " + phone + " - " + adress + " - " + country;
// alert(fullValues);
//2. Appel Ajax pour envoyé et traité les données en méthod GUEST vers le fichier manageGuest.php
$.ajax({
data:{
firstname: firstname,
lastname: lastname,
phone: phone,
email: email
},
url: '/core/manageGuest.php',
dataType: 'text',
type: 'GET',
success:function(response){
console.log("Email not Found ! Compte Created");
},
error:function(response){
console.log("Email Found ! Try Again ");
}
})
});
PHP :
$conf= Conf::$databases['default'];
$connectionString= "mysql:host=". $conf['host'] .";dbname=". $conf['database'];
try {
/* 1. Connection à la base de donnée */
$db = new PDO($connectionString, $conf['login'], $conf['password']);
//set the PDO error mode to exception
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$lastname = $_GET['lastname'];
$firstname = $_GET['firstname'];
$email = $_GET['email'];
$tel = $_GET['phone'];
/* 2. verification si l'email existe dans la BDD*/
$query = $db->prepare("SELECT email
FROM guest
WHERE email=:email");
$query->execute(array('email'=>$email));
$query->fetch(PDO::FETCH_OBJ);
$result = $query->fetch(PDO::FETCH_OBJ);
if(empty($result)){
$query = $db->prepare("INSERT INTO guest (firstname, lastname , tel, email)
VALUES('$firstname','$lastname', '$tel', '$email')");
$query->execute();
echo "Le compte été créé";
} else {
echo "Cette email existe déjà";
}}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();}