Page 1 of 1

How To check if username is already in the database

Posted: Wed Feb 24, 2010 1:46 pm
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

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

Posted: Wed Feb 24, 2010 1:57 pm
by AbraCadaver
Stopped what?

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

Posted: Wed Feb 24, 2010 1:59 pm
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.

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

Posted: Wed Feb 24, 2010 2:37 pm
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