View Single Post
  #6 (permalink)  
Old May 10th, 2007, 20:30
as22607 as22607 is offline
Junior Member
Join Date: May 2007
Location: britain
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Login Script problems

Quote:
Originally Posted by Donny Bahama View Post
Personally, I'd say you're also much better off with this:
Code: Select all
if (condition1) {
    // do this
} elseif (condition2)  {
    // do that
} else {
    // do something else
}
as opposed to this
Code: Select all
if (condition1)
{
// do this
}
elseif (condition2)
{
// do that
}
else
{
// do something else
}
It's a lot easier to spot problems in the structure that way.

Here's how I would have written the initial code you posted:
Code: Select all
<html>
<title>Login Checking</title>
<?php
$user="root";
$password="no";
$database="college";//connect to the database college using root and no password mysql_connect("localhost",$user);
@mysql_select_db($database) or die( "Unable to select database");//if database can not be found it rejects the connection otherwise connects to the desired database
/* better still to put the 4 lines above in a separate file such as db.connect.php then use an include statement here like so:
include 'db.connec.php';
*/
// this bit grabs the username and password from the form data
$username=$_POST['username'];    //grabs username from the previous form in post
$password=$_POST['password'];    //grabs password from the previous form in post
// this checks to see if either the password or username is blank or both are blank if they are it sends the user to the error page
if (empty($username) || empty($password)) {
    //check form to see if the password or username have been left blank or both
    header("Location: error.php");    //if the form is blank in any way it rejects the user and sends them to a error page
} else {
    // this checks to see if the username exists in the database if it doesnt it sends the user to the error page
    $query="select * from login where username = '$username'";    //does a query on mysql to see if the username exists or if there is multi occurences of the username
    $result=mysql_query($query);//put the results on the query into a variable
    $num=mysql_num_rows($result);//check the previous variable to see how many rows it returns
    if($num != 1) {
        //checks to see if the rows returns is either 0 or 2 or more
        header("Location: error2.php"); //if the rows is not 1 it send the user to another error page
    } else {
        //this checks the username and password match within the database if they dnt it sends the user to the error page, if it does match it sends them to the welcoem page.
        $password2=$_POST['password'];        //grabs password from the previous form in post as the first instance of it is lost so is required for the next check
        $query2="select * from login where username = '$username' and password = '$password2'";     //does a query on mysql to see if the username exists and wether the password is the same for that username
        $result2=mysql_query($query2);        //put the results on the query into a variable
        $num2=mysql_num_rows($result2);        //check the previous variable to see how many rows it returns
        if($num2 == 1) {
            //checks to see if the rows returns is 1
            header("Location: welcome.php");    // if the rows returned is 1 it sends the user to the welcome page
        } else {
            header("Location: error3.php");        // if the rows returned is 0 it sends the user to another error page
        }
    }
}
?>
</html>
(By the way, that should work as is.)
unfortnally this is not working and it is doign the same as before with my first post

thank you for the advice on making the code better by connecting to the database once though i thought you had to conenct ever time you make a query
Reply With Quote