Jump to content

[SOLVED] check if username is unique


Trium918

Recommended Posts

Problem: write a basic script that will check

if username is unique. This script works but I

am puzzled by the fact the script only works

like it is. Why?

 

Note: Just ideas

 

<?php
if ($reg_result) // will not work
if ($reg_result == true) // will not work

if ($reg_result == "true")
   {
     echo "<p class=\"genmed\">Your registration was successful.  Go to the members page "
          ."to start setting up your bookmarks!</p>";
   }
?>

 

<?php
// check if username is unique 
  $user_name = $_POST['user_name'];
  $result = mysql_query("select * from members_info where user_name='$user_name'"); 
  if (!$result)
     return "Could not execute query". mysql_error();
  if (mysql_num_rows($result)>0) 
     return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";
?>

Link to comment
Share on other sites

when using true or false, you should use 3 =

 

eg:

 

if ($reg_result === true)

 

But anyway, where do you come up with $reg_result, I dont see it anywhere else in the code you supplied.

 

<?php
// attempt to register
   $reg_result = register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day,
   $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code);
   if ($reg_result == "true")
   {
          echo "<p class=\"genmed\">Your registration was successful.  Go to the members page "
          ."to start setting up your bookmarks!</p>";
   }
?>

Link to comment
Share on other sites

I take it somewhere in your register function when registration is successful you have

 

return true;

 

If not, you better post your function

 

If so, did you try the 3= ?

 

if ($reg_result === true)

 

 

 

 

Link to comment
Share on other sites

do a

<?php
var_dump($reg_result);
die;
if ($reg_result == "true")
?>

 

i think your find register() is returning a string not a boolean,

 

as for the ===, this forces the check to be of the same type

 

ie

if(1=="1") // works

if(1==="1")//fails

 

 

 

that is if s/he uses return true;  and in what context (1/0, true/false)

Link to comment
Share on other sites

do a

<?php
var_dump($reg_result);
die;
if ($reg_result == "true")
?>

 

i think your find register() is returning a string not a boolean,

 

as for the ===, this forces the check to be of the same type

 

ie

if(1=="1") // works

if(1==="1")//fails

 

 

You're right it is returning a string!

<?php
return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";
?>

Link to comment
Share on other sites

yeah but as shaunrigby pointed out when using booleans === is important as

 

<?php
$test="false"
if($test) // will pass
if($test == true) // will pass
if($test === true) // will fails

?>

 

So what you are saying is that it is better

to use the method that I already have than

if($test === true) // will fails

Link to comment
Share on other sites

<?php
function register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day,
   $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code)
// register new person with db
// return true or error message
{
// connect to db
  $conn = db_connect();
  if (!$conn)
    return "Could not connect to database server - please try later.";

  // check if username is unique 
  $user_name = $_POST['user_name'];
  $result = mysql_query("select * from members_info where user_name='$user_name'"); 
  if (!$result)
     return "Could not execute query". mysql_error();
  if (mysql_num_rows($result)>0) 
     return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";

?>

Link to comment
Share on other sites

<?php
function register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day,
   $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code)
// register new person with db
// return true or error message
{
// connect to db
  $conn = db_connect();
  if (!$conn)
    return "Could not connect to database server - please try later.";

  // check if username is unique 
  $user_name = $_POST['user_name'];
  $result = mysql_query("select * from members_info where user_name='$user_name'"); 
  if (!$result)
     return "Could not execute query". mysql_error();
  if (mysql_num_rows($result)>0) 
     return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";

?>

Link to comment
Share on other sites

<?php
if (mysql_num_rows($result)>0)
{
     return false;
}else{
     return true;
}
?>

 

 

 

 

 

 


<?php
// attempt to register
   $reg_result = register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day,
   $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code);
   if ($reg_result === true)
   {
          echo "<p class=\"genmed\">Your registration was successful.  Go to the members page "
          ."to start setting up your bookmarks!</p>";
   }else{
echo "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";
}
?>

Link to comment
Share on other sites

Change :

$conn = db_connect();
 if (!$conn)
   return "Could not connect to database server - please try later.";

 // check if username is unique 
 $user_name = $_POST['user_name'];
 $result = mysql_query("select * from members_info where user_name='$user_name'"); 
 if (!$result)
    return "Could not execute query". mysql_error();
 if (mysql_num_rows($result)>0) 
    return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>";

 

To:

 

$conn = mysql_connect($host,$username,$password)
or die("Could not connect to database server - please try later." . mysql_error());//Remove . mysql_error() after you have finished developing your code

mysql_select_db($dbname)
or die(mysql_error());

 // check if username is unique 
//  $user_name = $_POST['user_name'];//Why you setting this? you are obtaining it VIA your function,
 $query = mysql_query("SELECT * FROM `members_info` WHERE `user_name` = '" . $user_name . "'")
or die(mysql_error());

$result = mysql_num_rows($query);

 if ($result > 0 ) {
    return true;
} else {
return false;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.