0

and i just got a basic registration system for my website, and i can't figure out how to disallow duplicate usernames, can anyone help me?

<?php
  if(!empty($_POST['email']) && !empty($_POST['password'])){
$query = "INSERT INTO users (name, email, password) VALUES (:name, :email, :password)";
$stmt = $conn->prepare($query);

$stmt->bindValue(':name', $_POST['name']);
$stmt->bindValue(':email', $_POST['email']);
$stmt->bindValue(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));

if( $stmt->execute() ){
  echo 'Success';
} else {
  echo 'Failure';
  print $stmt->errorCode();
}

}

?>
Xexxi
  • 3
  • 1

1 Answers1

0

You prevent duplicate user names by defining a unique index/constraint on the table:

create unique index unq_users_name on users(name);

Any attempt to insert or update values resulting in a name appearing twice will result in an error, and the insert will fail.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786