7 Replies - 556 Views - Last Post: 22 October 2013 - 09:05 PM Rate Topic: -----

#1 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Username Availability in Database

Posted 19 October 2013 - 09:38 PM

A little background on my PHP script. It is not a finished product, I am just looking for a little insight. If there is something wrong with my MySQL statement then please, let me know. Basically what I am trying to do is when the user is registering for the website, I want to be able to check if the username is available after typing has stopped for 2 seconds.

Here is my PHP function to check the database:


<?php
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];



function checkAvailability($username){
    if(isset($_POST['username'])){
        $stmt = $mysqli->prepare("SELECT Username FROM users WHERE Username=? LIMIT 1");
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->bind_result($username);
        $stmt->store_result();
        if($stmt->num_rows == 1){
            echo "Available"            
        }else{
            echo "Unavailable"
        }
        $stmt->free_result();
        $stmt->close();
    }
}


?> 



Now here is where I am asking for a little advice. I know I have to use AJAX to access my PHP file, and I planned on using Javascript to display if it is available or unavailable by echoing the javascript function to change the div. My advice needed is, should I create a function of javascript to call the AJAX query for my PHP file? Or is there a simpler way to do it?

Also take note that I will be using this login PHP file for checking if the username and/or password meets certain criteria. So should I just run it all at once? Or do it as they type values into the form fields?


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Username Availability in Database

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Username Availability in Database

Posted 19 October 2013 - 11:06 PM

Hi!

Good job using MySQLi and prepared statements! Yeah you could create a library of javascript functions including one you mentioned above (checking the availability of a username), but if it's only going to be used in that one spot you could just code for that. I suggest using jQuery to be honest, it'll make your life a ton easier. Checking would simply be something like:

$.get('checkAvailability.php', { username: "the username" }, function(result){
    $('#username_availability').html(result);
});


Was This Post Helpful? 1
  • +
  • -

#3 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: Username Availability in Database

Posted 19 October 2013 - 11:18 PM

JQuery goes right in my HTML and not my Javascript Library I am assuming?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: Username Availability in Database

Posted 20 October 2013 - 12:50 AM

it goes whereever you like it. itís only another piece of Javascript.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Username Availability in Database

Posted 20 October 2013 - 02:13 AM

Database wise, I'd prefer:
SELECT count(*) FROM users WHERE Username=?



Not only is this more portable, it's more predictable. ( And probably quicker. ) You know that your result will either by 0 or 1.
Was This Post Helpful? 0
  • +
  • -

#6 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: Username Availability in Database

Posted 22 October 2013 - 06:51 PM

Thank you all for the help. :)

This post has been edited by bteeple: 22 October 2013 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: Username Availability in Database

Posted 22 October 2013 - 07:10 PM

Alright, I got my register/login PHP script built. I just wanted to get a second look at it. Any insight or improvements would be great. :)/>/>

And just wondering, how would the count(*) be faster? How is it different than what I have now? Also, is it smart to put both the login and registration in one file? Or should I split it up?

<?php
$mysqli = new mysqli("", "", "", "");
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];


function userStart($type){
    $availability = false;
    if($type == "register"){
        $availability = checkAvailability($username);
        if($availability == true){
            userRegister();
        }else{
            header('Location: register.php')
        }
    }else if($type == "login"){
        userLogin();
    }
    
}
function checkAvailability($username){
    $returnvalue = false;
    if(isset($_POST['username'])){
        $stmt = $mysqli->prepare("SELECT Username FROM users WHERE Username=? LIMIT 1");
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->bind_result($username);
        $stmt->store_result();
        if($stmt->num_rows == 1){
            $returnvalue = true;           
        }else{
            $returnvalue = false;
        }
        $stmt->free_result();
        $stmt->close();
    }
    return $returnvalue;
}
function userLogin(){

    if(isset($_POST['username']) && isset($_POST['password'])){
        $stmt = $mysqli->prepare("SELECT Username, Password FROM users WHERE Username=? AND  Password=? LIMIT 1");
        $user = $_POST['username'];
        $pword = $_POST['password'];
        $stmt->bind_param('ss', $user, $pword);
        $stmt->execute();
        $stmt->bind_result($user, $pword);
        $stmt->store_result();
        if($stmt->num_rows == 1) 
            {
                   header('Location: game.php');
            }
            $stmt->free_result();
            $stmt->close();
            
        }
        else 
        {   
		header('Location: register.php');
        }
}
function userRegister(){
    if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])){
        $stmt = $mysqli->prepare("INSERT INTO users (Username, Password, Email) VALUES ('$username','$password','$email')");
        $stmt->bind_param('sss', $username,$password,$email);
        $stmt->execute();
        $stmt->bind_result();
        $stmt->store_result();
        if($stmt){
            header('Location: home.php');
        }else{
            header('Location: register.php');
        }
    }
    
}
$mysqli->close();
?>

This post has been edited by bteeple: 22 October 2013 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: Username Availability in Database

Posted 22 October 2013 - 09:05 PM

that code shouldnít work at all.
- variables are used out of scope (lines #11, #25, #43, #65)
- query is not a prepared statement (line #65)
- line #70 is always true ($stmt is always an object)

Quote

And just wondering, how would the count(*) be faster?

because SQL is good at such operations. and you donít need to count your result set, not to speak of the data you donít need to transfer which you do not use anyways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1