Change this to use SQL

Discussion in 'PHP' started by adamjblakey, Nov 16, 2007.

  1. #1
    Hi,

    I want to change this so instead of using the values that is there e.g. if ( $username == "joe" || $username == "rony" || $username== "joo" ) I want to search a MySQL table called users to see if there is a match.

    <?
    $action = trim($HTTP_GET_VARS['action']);
    ?>
    <?
    switch ( $action ):
     case 'validateUsername':
       $username = trim($HTTP_GET_VARS['username']);
       if ( strlen($username) >= 3 ){
       	   if ( $username == "joe" || $username == "rony" || $username== "joo" )
    			print "<font color=GREEN>Username is valid</font>";
    	   else
    			print "<font color=RED>Username is not valid</font>";
    	}
    	else{
    		print "<font color=RED>Username must be longer then 3 characters</font>";
    	}
     break;
     default:
     //silence is golden
    endswitch;
    ?>
    Code (markup):
    Thank you in advanced for your help.
    Cheers,
    Adam
     
    adamjblakey, Nov 16, 2007 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    Hi,

    you can probably try this

    SELECT * FROM users WHERE name_of_field IN ('joe','rony','joo')

    OR

    SELECT * FROM users WHERE name_of_field = 'joe' OR name_of_field = 'rony' OR name_of_field = 'joo'
     
    serialCoder, Nov 16, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Sorry i did not explain my self properly, what i meant is i want to check if the POST user name exists in the users table. So i don't want to check with certain names just all entries.
     
    adamjblakey, Nov 16, 2007 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    oh ok,

    try this then

    SELECT * FROM users WHERE name_of_field = '".$_POST['form_field_name']."'"

    Note: having the post directly in the query is not at all safe but i've used it here for brevity
     
    serialCoder, Nov 16, 2007 IP
  5. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #5
    $result = mysql_query("SELECT * FROM users WHERE  user_name='$_POST[username]'");
    if(mysql_num_rows($result)){
    // show error msg here
    }
    else
    {
    // welcome new user 
    }
    PHP:
     
    greatlogix, Nov 16, 2007 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    I have now done this but it tell me the username is available even if it is not.

    <?php
    include("config/db.config.php");
    $action = trim($HTTP_GET_VARS['action']);
    
    switch ( $action ):
     case 'validateUsername':
       $username = trim($HTTP_GET_VARS['username']);
       if ( strlen($username) >= 3 ){
       
       $result = mysql_query("SELECT * FROM users WHERE username='$_POST[username]'");
    		if(mysql_num_rows($result)){
    		print "<font color=RED>Username has already been taken.</font>";
    		}else{
    		print "<font color=GREEN>Username is available</font>";
    		}
    
    	}else{
    		print "<font color=RED>Username must be longer then 3 characters</font>";
    	}
     break;
     default:
     //silence is golden
    endswitch;
    ?>
    Code (markup):
     
    adamjblakey, Nov 16, 2007 IP