homepage Welcome to WebmasterWorld Guest from 54.196.162.238
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Form validation
Username is available
Adam5000




msg:4210041
 4:17 pm on Oct 1, 2010 (gmt 0)

Continuing with form validation

After I have some usernames in my database I'd like to check the database to see if the username the user has chosen is available and not already in use by a current user.

My thoughts are to use PHP to gather values from the database and then use a PHP if / else statement to compare these values to the one the user has chosen and see if there are any matches.

The code I've got so far is below.

Help!

<html>
<head>
<title>Check username</title>
</head>

<body>

<form action = "insert_data.php" method = "post">
<input type = "text" name = "user_name">
<input type = "submit" value = "Submit">
</form>


<?php
$con = mysql_connect("host_address","database_name","password");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database_name", $con);


if ($_POST["user_name"] == any value in column "username" in the database)

echo "Username not available."

else

{
echo "Username available."
}

?>

</body>
</html>

 

Matthew1980




msg:4210079
 5:14 pm on Oct 1, 2010 (gmt 0)

Hi there.

Very close. Try this:-

" SELECT `username` FROM `table` WHERE `username` = '".$_POST['username']."' "
Then use mysql_num_rows() to see if the query yeilds a result (ie if it returns more than 0). Hope that makes sense.

Cheers,
MRb.

Adam5000




msg:4210428
 1:37 pm on Oct 2, 2010 (gmt 0)

Matthew! You're a genius! The Where condition and the mysql_num_rows() function were just what was needed. You're brilliant! Applause to you!

Below is the code that worked.

<html>
<head>
<title>Username validation</title>
</head>

<body>

<form action = "username_validation.php" method = "post">
<input type = "text" name = "user_name">
<br>
<input type = "submit" value = "Submit">
</form>



<?php
$con = mysql_connect("host_address","database_name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database_name", $con);

$result = mysql_query("SELECT * FROM subjects WHERE username = '$_POST[user_name]'");

if (mysql_num_rows($result) > 0)

echo "Username not available";

else
{
echo "Username available";
}

mysql_close($con);
?>

</body>
</html>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved