Answer the question
In order to leave comments, you need to log in
Checking the uniqueness of a record, how does it work?
Hello, please explain how the uniqueness of a database record works, or rather its verification?
I want it to throw an error when a certain record in the database matches. To do this, I set the Index for the column. But so far, unfortunately, one option works, it's too crooked. The second one doesn't work at all.
1st option:
$stmt = $dbh->prepare("INSERT INTO users (name, lastname, email, password) VALUES (:name, :lastname, :email, :pass)");
$stmt->bindParam(':name', $name_t);
$stmt->bindParam(':lastname', $lastname_t);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':pass', $hashed_password);
$falg = $stmt->execute();
if ($falg) {
header ("Location: index.html");
die;
} else {
echo "Данная электронная почта уже используется. ";?><a href="enter.php" style="color: red">Войдите</a> <?php echo "по этой почте или используйте другую";
die;
}
$stmt = $dbh->prepare("INSERT INTO users (name, lastname, email, password) VALUES (:name, :lastname, :email, :pass)");
$stmt->bindParam(':name', $name_t);
$stmt->bindParam(':lastname', $lastname_t);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':pass', $hashed_password);
$stmt->execute();
if ($stmt->fetch(PDO::FETCH_NUM)) {
echo "Данная электронная почта уже используется. ";?><a href="enter.php" style="color: red">Войдите</a> <?php echo "по этой почте или используйте другую";
}else{
echo "Успешно";
}
if ( !empty($pwd) && !empty($email) ) {
$stmt = $dbh->prepare('SELECT email, password FROM users WHERE email = :email');
$stmt->bindParam(':email', $email);
$stmt->execute();
$user_p = $stmt->fetch(PDO::FETCH_OBJ);
if ($user_p){
if(password_verify($pwd, $user_p->password)){
echo "Успешно";
}else{
echo '<p class="wrong">Неверный Email или пароль</p>';
}
}else{
echo '<p class="wrong">Неверный Email или пароль</p>';
}
}else{
echo '<p class="noValues">Пожалуйста, заполните все поля</p>';
}
Answer the question
In order to leave comments, you need to log in
I want it to throw an error when a certain record in the database matches. To do this, I set the Index for the column.The email field in the database must be UNIQUE INDEX, then when inserting a duplicate, MySQL will throw an error that can be processed.
INSERT IGNORE ... ON DUPLICATE KEY
, for example, to return the ID of the record with this email.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question