Jump to content

PHP Username Checking Help Request


herghost

Recommended Posts

Hi all,

 

I am wondering if someone could help me out with some PHP code. I have a form that is meant to check if a username is available using ajax and this file:

 

The username '<?php echo $_POST["username"]; ?>' <strong>is
<?php
if($_POST["username"] == "admin")
{
?>
not
<?php
}
?>
</strong> available.

 

 

This works fine, however instead of having to manaully update this list everytime someone registers I want it to check my php database. This is where my noobness comes into play!

 

So far I have this:

 

The username '<?php echo $_POST["username"]; ?>' <strong>is
<?php
$query = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   if(!$query) {
      die('Failed to connect to server: ' . mysql_error());
   }
   
   
   $db = mysql_select_db(DB_DATABASE);
   if(!$db) {
      die('Unable to select database: ' . mysql_error());
   }
   
?>
not
<?php
}
?>
</strong> available.

 

Which just connects to the database!

 

I know that I am going to need to pull the values from the table, all the members names are in a coloum called members and then loop through them checking each one, I just dont know where to start

Link to comment
https://forums.phpfreaks.com/topic/149540-php-username-checking-help-request/
Share on other sites

Just query the user name.

 

$sql = mysql_query( "SELECT members FROM table WHERE members='" . $_POST["username"] . "'", $query );
if( mysql_num_rows( $sql ) > 0 )
    echo "not";

 

That should do the job. I would also think about putting some SQL injection prevention in place.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.