-1

I have one field named

username:_________ now i want to add manuaaly username to the database if once added and i add same next time it must automatically show invalid username right side of textbox i am searching same code on net and tried too many solution but not working.

This i want to performed in php and mysqli

yogi
  • 53
  • 1
  • 7
  • 1
    what you have tried ? post your coding... with out coding we can't give a solution – Arun Feb 25 '15 at 11:58
  • I have not started yet I have just given unique constraint to the username field in my table – yogi Feb 25 '15 at 12:02

1 Answers1

1

Just mark it as UNIQUE.

CREATE TABLE User
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

And in PHP script do something like this:

$query = mysqli_query($con, "SELECT id FROM user WHERE username='$username'");

if(mysqli_num_rows($query) > 0){

    echo "Username already exists";
}else{
    // do something
    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
}

And if you want to check in realtime if username is available (like @Arun pointed) maybe this would help a bit.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • I want to display to the user as soon as he finished typing the username and go to the next field it must displayed to him duplicate username enter different like message – yogi Feb 25 '15 at 12:01
  • 1
    you have to try in Jquery and Ajax – Arun Feb 25 '15 at 12:03
  • please refer this link http://www.sanwebe.com/2013/04/username-live-check-using-ajax-php for username availablity – Arun Feb 25 '15 at 12:30