login script

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
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

login script

Post by lawler_09 »

hi guys,

any help will be much appreciated!!

basically i have a login script, that i want to check mutliple tables and i am stuggling to get it to work!

its for a college project, and basically theres 4 different types of accounts, with different fields in each.

when someone enters an email address and password, i want the script to check each of the 4 tables for what was entered, and then forward them somewhere depending on which table it was found in, or if it wasnt found in any then to another page

Code: Select all

<?php
   session_start();
   $_SESSION['loggedin'] = false;
   include("functions.php");
   extract($_POST);
   
   $query = "SELECT * From table1 WHERE email='$email' and password='$password';";
   $result = doQuery($query);
   if($result==false)   {
      
      $query = "SELECT * From table2 WHERE email='$email' and password='$password';";
   $result = doQuery($query);
   if($result==false)   {
      
      
      $query = "SELECT * From table3 WHERE email='$email' and password='$password';";
   $result = doQuery($query);
   if($result==false)   {
      
      
      
      $query = "SELECT * From table4 WHERE email='$email' and password='$password';";
   $result = doQuery($query);
   if($result==false)   {
      
   }   else {
         if(mysql_num_rows($result))   {
            $row = mysql_fetch_assoc($result);
            $_SESSION['loggedin'] = true;
            $_SESSION['id'] = $_POST['id'];
            header("Location:1");
   }   else {
         header("Location:wrong.php");
   }
   }   
      
   }   else {
         if(mysql_num_rows($result))   {
            $row = mysql_fetch_assoc($result);
            $_SESSION['loggedin'] = true;
            $_SESSION['id'] = $_POST['id'];
            header("Location:2");
   }   else {
         header("Location:wrong.php");
   }
   }   

   }   else {
         if(mysql_num_rows($result))   {
            $row = mysql_fetch_assoc($result);
            $_SESSION['loggedin'] = true;
            $_SESSION['id'] = $_POST['id'];
            header("Location:3");
   }   else {
         header("Location:wrong.php");
   }
   }   
         
   }   else {
         if(mysql_num_rows($result))   {
            $row = mysql_fetch_assoc($result);
            $_SESSION['loggedin'] = true;
            $_SESSION['id'] = $_POST['id'];
            header("Location:4");
   }   else {
         header("Location:wrong.php");
   }
   }   
?>
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: login script

Post by Pazuzu156 »

By the look of the code, you haven't connected to a database.

Code: Select all

<?php
$host = "localhost";
$user = "root";
$pass = "";
$database = "db_name";
$table = "tbl_name";

$connect = mysql_connect("$host","$user","$pass") or die ("Cannot connect to database host");
$selDB = mysql_select_db("$database") or die ("Cannot find database");
$queryget = mysql_query("SELECT * FROM $table") or die ("Invalid Query");
?>
If you are looking for a good help, here is a link you should check out: http://www.youtube.com/watch?v=4oSCuEtxRK8
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: login script

Post by phazorRise »

It should not be done like this.
First create fifth table and create three columns. First two for email and password. Thirds column for the type of account they belong to. Check this table for any entry instead of traversing through all four tables. And if both email and password match with user entered values then reteive third column value and based upon that redirect user to his destination.
as a new user is register make an entry in fifth table also with his account type.
Post Reply