Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Checking for duplicate records in database

    i got a form
    Code:
    <form method="post" action="haha.php">
    username<input type="text" name="username">
    <br>
    email<input type="text" name="username">
    <br>
    <input type="submit" value="submit">
    </form>
    now i want to add a button beside the username field to check whether the user name is duplicated in database, what to d? please give a suggestion, thanks
    flying dagger

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I think this is beyond just PHP, the besy way to do it is to submit and if the name exists in the database you would echo out the same form with the $_POST information they submitted and put a note next to the name field and have them change it.

    The easiest way to check if a user exists is like this:

    PHP Code:

    if(mysql_query("SELECT COUNT(id) FROM users WHERE username=`".$_POST['username']."`")) {
      
    $user_exists true;

    then wherever you want to echo the form if the user exists you check if $user_exists is true and then if so echo the form again. You could even do it right in that if statement above.

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Hi, thanks, i used to do your method, but i got see this kind of function in quite a lot of sites and i feel want to know the technology behind
    flying dagger

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I'm sure its AJAX and PHP. I havn't used AJAX besides Velox example so I don't think I could successfully help you there.

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Element
    PHP Code:

    if(mysql_query("SELECT COUNT(id) FROM users WHERE username=`".$_POST['username']."`")) {
      
    $user_exists true;

    That won't work becouse an mysql_query returns true regardless of the result set being empty or not... in fact it will always return something. COUNT(id) will be just equal 0. Moreover the query won't work as you used backticks instead of quotes.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Your thread title is vague. Please read our posting guidelines
    http://www.codingforums.com/postguide.htm
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    sorry, i tried to change the title, but cannot
    flying dagger


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •