How To check if username is already in the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jay_bo
Forum Newbie
Posts: 4
Joined: Wed Feb 24, 2010 11:08 am

How To check if username is already in the database

Post by jay_bo »

Can someone tell me where i have gone wrong? i had it working but not it has stopped.

Code: Select all

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password= ""; // Mysql password
$db_name="computershop"; // Database name
$tbl_name="login"; // Table name
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
session_start();
 
// username and password sent from form
$firstname = $_POST["firstname"]; //Stores the data into the variables
$lastname = $_POST["lastname"]; //Stores the data into the variables
$email = $_POST["email"]; //Stores the data into the variables
$myusername = $_POST["username"]; //Stores the data into the variables
$mypassword = md5($_POST["password"]); //encrypts the password that has been entered by the user
 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' or email='$email'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
$row = mysql_fetch_row($result);
if($count==1){
$_SESSION["Error"]=true;
header("location:signup.php");//Sends the user back to the sign up page due to incorrect details
}
else {
 
//insert the values
$result= MYSQL_QUERY("INSERT INTO login (id, username, password, firstname, lastname, email)".
   "VALUES ('NULL', '$myusername', '$mypassword', '$firstname', '$lastname', '$email')");
}
    session_start();
    $_SESSION["myusername"]=="";
    session_destroy(); //clears sessions  
?>
Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How To check if username is already in the database

Post by AbraCadaver »

Stopped what?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: How To check if username is already in the database

Post by Kurby »

Are you checking if a username already exists or a login script? Your query doesn't contain any WHERE with a password.

You should also use mysql_escape_string no your inputs.
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: How To check if username is already in the database

Post by lshaw »

What actually happens? is there an error or does it just not ever stop a user signing up?

You have two session_start() calls too.

Is there a reason you have quotes around these variables?

Code: Select all

 
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
why do you need to fetch_row()? you never use this again

I cant really see any errors though

Lewis
Post Reply