1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Check Function

Discussion in 'PHP' started by K0307277, May 7, 2007.

  1. #1
    Hi guys,
    I am quite new to PHP and MySQL. I have created a form for a booking form and i want to check the username is valid against one in the database. If it is not valid i would like an error message to come up. Does anyone know the syntax for this?:) :)
     
    K0307277, May 7, 2007 IP
  2. Wyla

    Wyla Well-Known Member

    Messages:
    924
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    140
    #2
    $username = $_POST['user']; //Gets username from the form
    $query = mysql_query("SELECT * FROM `tablename` WHERE `user` = $username"); //Searches table "tablename" for user
    $num = mysql_affected_rows($query);//Counts the amount of rows returned/affected
    if($num != 0 ){ //if # of rows affected is not equal to 0
     echo "user found!"; //user found
    }else{ //otherwise
     echo "user NOT found!"; //user not found
    }
    PHP:
     
    Wyla, May 7, 2007 IP
  3. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #3
    small issue: sql injection

    use this code instead
    
    $query = mysql_query("SELECT * FROM `tablename` WHERE `user` = '".mysql_real_escape_string(stripslashes($username))."');
    
    PHP:
    Another issue
    In your case (SELECT command) instead of mysql_affected_rows, use mysql_num_rows($query);
     
    gibex, May 10, 2007 IP