How to Prevent Duplicate Usernames When People Register

How to prevent duplicate usernames when people register?

You can do it like this when the user post the username for example and click submit you can write this code using mysqli:

<?php

// make sure the error reporting is enabled!
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'password', 'test');
$mysqli->set_charset('utf8mb4');

$username = $_POST['username'];
$stmt = $conn->prepare("SELECT 1 FROM table_name where username=?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_row();
if ($user) {
$error[] = "This username is already taken!";
}

When you create the column of the user you can make it unique, for example create table users(username varchar(350) not null unique).

Prevent duplicate usernames when people register

Depends what database you use.

A simpler way would be to just create a unique constraint on the table, this will also enforce it for updates too and remove the need for a trigger. Just do:

Example for MSSQL:

ALTER TABLE [dbo].[TableName]    
ADD CONSTRAINT [UQ_ID_Name_Date] UNIQUE NONCLUSTERED
(
[Name], [Date]
)

and then you'll be in business.
When you create trigger like this in database - there will be impossible to create users with the same Username.

Preventing duplicate username entries when registering (PHP/MySQL

You can also set username as unique key index which will prevent duplicate rows to enter in table.

and review code below

<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$username = stripslashes($username);
$username = mysql_real_escape_string($username);
$email = stripslashes($email);
$email = mysql_real_escape_string($email);
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date) VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysql_query($query);
if (mysql_errno()) {
if (mysql_errno()==1062) {
echo "Username already taken.";
}
}else{
if($result){
echo "You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}
}else{
?>

Hope this works for you....

How to prevent duplicate Usernames in php/mysql

<?php
include('connection.php');

if(isset($_POST['form'])){
if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['conf_pass']) || empty($_POST['email'])){
echo '<b>Please fill out all fields.</b>';

}elseif($_POST['password'] != $_POST['conf_pass']){
echo '<b>Your Passwords do not match.</b>';
}else{

$dup = mysql_query("SELECT username FROM users WHERE username='".$_POST['username']."'");
if(mysql_num_rows($dup) >0){
echo '<b>username Already Used.</b>';
}
else{
$url = 'http://graves-incorporated.com/test_sites/plantation_park_2012/';
echo '<META HTTP-EQUIV=Refresh CONTENT="2; URL='.$url.'">';

$sql = mysql_query("INSERT INTO users VALUES(NULL, '$_POST[username]', '$_POST[password]', '$_POST[email]')");
if($sql){
echo '<b>Congrats, You are now Registered.</b>';
}
else{
echo '<b>Error Registeration.</b>';
}
}
}
}
?>

How to prevent duplicate username and email data?

Create a UNIQUE CONSTRAINT on email_user and username column in your tb_user table; which will throw error if you try to insert duplicates.

ALTER TABLE tb_user ADD CONSTRAINT constr1 UNIQUE (email_user);

Python flask - how to make it so there can't be duplicate usernames registered in the MySQL database

Generally, you have two options:

  1. Make the verification exclusively in your Flask application code.

  2. Use database features to block duplicate users to be inserted (e.g. add a unique key constraint on the username attribute).

If you're going to use 2) you need to handle in your Flask code the error returned by the DBMS when it detects that someone is trying to add a new row with an existing username value.

Otherwise, for option 1), you could do something similar to what you do in your login route:

cursor.execute("SELECT * FROM logininfo WHERE Username=%s", (username,))
existingInfo = cursor.fetchone()
if existingInfo is not None:
return "Error: user is already registered" # or render_template a special error template.


Related Topics



Leave a reply



Submit