how to restrict double inclusion of data?

Discussion in 'PHP' started by khan11, Jan 8, 2009.

  1. #1
    Hello guys!

    I'm creating a login/logout script, the script is working fine but it is not very well featured, i would like to put a restriction that when someone tries to signup with same username or same email that is signed up before, it should give an error.

    I tried somefin like if($fieldname['username'] == $_POST['username']) { // give an error }

    but this doesn't seem to work. $fieldname is an array fetching data from mysql. and $_POST['username'] is the username that will be used on signup, please guide me on where i'm wrong or what should i do to put a restriction..

    thanks in advance.
     
    khan11, Jan 8, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Something like...

    $username = mysql_real_escape_string(get_magic_quotes_gpc() ? stripslashes($_POST['username'] : $_POST['username']);
    PHP:
    Then a query similar to...
    SELECT count(id) as COUNT FROM database WHERE username = '$username' LIMIT 1;
    Code (markup):
    Then
    if(mysql_result($result, 0, 'COUNT') > 0) $errors[] = 'Username already exists.';
    PHP:
    This is MUCH faster than retrieving all the rows and using an in_array() to check.
     
    Danltn, Jan 8, 2009 IP
    khan11 likes this.
  3. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    wow, it worked.

    Thank you so much. About that mysql_real_escape.... thing, i already did that.

    Thanks anyways. + For you!
     
    khan11, Jan 8, 2009 IP