4 Replies - 446 Views - Last Post: 10 August 2011 - 01:45 PM Rate Topic: -----

#1 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Registration Code Always returning username unavailable

Posted 09 August 2011 - 02:49 PM

All,

I am trying to develop a login/registration function for a website that I am building, and have run into a few snags. The login portion works perfect, and I am now working on the registration part. The first thing I am trying to do is get the system to check and see if the requested username is available, and it tells me that it is not available no matter what I enter into the username text box. My code is below.

As always, any help is greatly appreciated.

Ed.

The registration form block:
<?php require_once($root_dir."membership/membership_functions.php"); ?>

<?php //<form action="<?php echo $root_dir; membership/registration_complete.php" method="post"> ?>
<form action="<?php echo $current_node; ?>" method="post">
    <table>
        <tr>
            <td colspan="3"><h3>Registration</h3></td>
        </tr>
        <tr>
            <td>Username:</td>
            <td><input type="text" name="reg_user" maxlength="30" value="<?php if ($_POST['submit_reg']) {echo $reg_user; } else {echo htmlentities($reg_user); } ?>" /></td>
            <td><?php if(!$user_name_avail) { echo "*"; } ?></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="text" name="password" maxlength="30" value="<?php echo htmlentities($reg_pass) ?>" /></td>
            <td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
        </tr>
        <tr>
            <td>Retype Password:</td>
            <td><input type="text" name="password_check" maxlength="30" value="<?php echo htmlentities($reg_pass_check) ?>" /></td>
            <td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" name="email" maxlength="30" value="<?php echo htmlentities($reg_email) ?>" /></td>
            <td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
        </tr>
        <tr>
            <td>Retype Email:</td>
            <td><input type="text" name="email2" maxlength="30" value="<?php echo htmlentities($reg_email_check) ?>" /></td>
            <td><?php if($user_name_avail) { echo "ok!"; } else { echo "bad!"; } ?></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" name="submit_reg" value="Register"/> </td>
        </tr>
    </table>
</form>


The membership_functions.php file.
<?php

if (!$root_dir) { $root_dir = "./"; } 
require_once($root_dir . "includes/connection.php");
    
// Registration Functions:
function check_name_available($name) {
    return 1;
}
    
function create_hash($value) {

    return hash(whirlpool, $value);     
}
    
function username_available($name) {
    
    $query .= "SELECT username FROM members ";
    $query .= "WHERE username = '{$name}'";
    $results = mysql_query($query, $mysql_conn);
    $rows = mysql_fetch_array($results);
    return $rows['username'];
    
}

if ($_POST['login_submit']) { // Form is posted for login.
     $message = "Post Submitted for Login";
    // Get Submitted Username and Password.
    $username = $_POST['username'];
    $password = $_POST['password'];
            
    // Get hash for password.
    $hashed_pass = create_hash($password);
            
    // Create query to see if user and password are in database.
    $query  = "SELECT * FROM members ";        
    $query .= "WHERE username = '{$username}' ";
    $query .= "AND password = '{$hashed_pass}'";
            
    // Get results of query
    $results = mysql_query($query, $mysql_conn);
    if(mysql_num_rows($results) > 0) { // results were found.
        $row = mysql_fetch_array($results);
        $_SESSION['user_id'] = $row['id'];
        $_SESSION['user_name'] = $row['username'];
    }
} elseif ($_POST['submit_reg']) { // This is a registration request.
    $message = "Post Submitted for registration";
    // Need to verify registration information.
    // Check to ensure unique username.
    $requested_name = $_POST['reg_user'];
    $name_is_available = username_available($requested_name);
    if (!$name_is_available) {
        //$reg_user = $_POST['reg_user'];
        $message .= "<br />Username Unavailable: " . $requested_name;
        $message .= "<br />Username Unavailable: " . $name_is_available;
        $user_name_avail = false;
    } else {
        //$reg_user = $_POST['reg_user'];
        $user_name_avail = true;
        $message .= "<br />Username Available: " . $requested_name;
        $message .= "<br />Username Available: " . $name_is_available;
    } 
    
} else { // This is the original load.
     $message = "Post Submitted for last else";
}
    
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Registration Code Always returning username unavailable

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Registration Code Always returning username unavailable

Posted 09 August 2011 - 03:00 PM

A query either returns the rows fetched or FALSE if the query failed. An empty result set is still considered to be a valid resource which means that in a comparison it evaluates to TRUE.

If you really want to check the username exists or not I'd suggest using MySQL's function COUNT which returns the amount of rows that match the condition.

Example
SELECT COUNT(`username`) AS `total` FROM your_table WHERE `username` = 'value'

This post has been edited by codeprada: 09 August 2011 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 qtex  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 142
  • Joined: 24-October 09

Re: Registration Code Always returning username unavailable

Posted 09 August 2011 - 05:50 PM

One way is to use mysql_num_rows.

function username_available($name) {
    
    $query .= "SELECT username FROM members ";
    $query .= "WHERE username = '{$name}'";
    $results = mysql_query($query, $mysql_conn);
    $rows = mysql_num_rows($results);
    return $rows;
    
}



if(username_available($name) == 0)
{
   echo "Username Available";
}
else
{
  echo "Username Not Available";
}


Was This Post Helpful? 0
  • +
  • -

#4 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Registration Code Always returning username unavailable

Posted 10 August 2011 - 01:03 PM

Guys,

I tried both ways, and still get nothing back. After beating my head on the desk for a little while, I realized that I am performing the check inside a function, and did not bring my connection variable into the function. after adding
global $mysql_conn;
to the function, I am actually pulling data now!

Thanks for the help.

Ed
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Registration Code Always returning username unavailable

Posted 10 August 2011 - 01:45 PM

Please don't use global.

If you have code that requires the use of global you should seriously consider using a class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1