Jump to content

[SOLVED] PHP Solution to not being able to duplicate usernames in SQL


plznty

Recommended Posts

It does not let me enable the username column to be unique.

So how can I go about checking if the username in the column "username" and if there is no duplicates then it gets created.

A example of a solution would be of much help, or a theory based comment.

Thank you !

Are you using a PHP script to do this?

 

If you insert with PHP you can run a query that elimates this from happening.

 

Let me known if you want a script developed. It can take seconds.

 

Yours,

George

Are you using a PHP script to do this?

 

If you insert with PHP you can run a query that elimates this from happening.

 

Let me known if you want a script developed. It can take seconds.

 

Yours,

George

 

If you could it would be appreciated.

Ok I am going to assume you have knowldege of PHP queries and POST variables etc so I am going to use a form as an example if someone is signing up.

 

 

The form:

 


<form name="register" method="post" action="register.php">

<fieldset>
<legend>Choose a Username and Password:</legend>

<label>Choose a Username:</label>
<input type="text" name="username">

<p> </p>

<label>Choose a Password:</label>
<input type="password" name="password">

<button type="submit">Register!</button>

</fieldset>

 

 

The Script:

 


<?php

//Database Connection Information Here
$server = "db server";
$dbuser = "dbuser";
$dbpass = "dbpass";
$database = "db"

//Set Variables
$username = $_POST["username"];
$password = $_POST["password"];

//Connect to database
$con = mysql_connect("$server","$dbuser","$dbpass");
$db = mysql_select_db("$database");

//Run Query
$query = "SELECT * FROM tablename WHERE username ='$username'";
$query = mysql_query("$query");
$check_username = mysql_num_rows($query);

//Check IF Username Exists

if("0" != $check_username) { $error_username = "1"; }

//Use Result

if("1" == $error_username) { header('Location: http://www.example.com/'); //Insert Error Page URL

else {

INSERT YOUR INSERT QUERIES IN HERE

}

?>

 

I hope that helps you and that you can understand it.

Let me know if you need help.

 

EDIT: Do you wish to do this in SQL or using a signup facility as if thats the case my script will not function as that checks for existance before inserting or editing databases with PHP. As otherwise what roopurt18 might work if you set your value to varchar with a high limit compared to the password.

table.png

It doesnt let me make it unique?

 

Seems that your table already has some data. Please check whether there exists duplicate username already in the table or not. If there is already duplicate username exists, you cannot set UNIQUE for the username column. Same goes for blank values in username column. Remove the duplicate username values and/or blanks from the table or change them to some unique values directly into the table, then it will work.

 

Hope this will help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.