-1

I have a username in table1 and password in table 2. What is the syntax to login?

How can I make a login function if I have two tables:

TABLE1 NAME: accounts | COLUMN: acc_id, password, mem_id
TABLE2 NAME: members  | COLUMN: mem_id, firstname

I want to make the "firstname" as $username and make "password" as $password. I have code here, but I can't continue as I don't know whether it's right or not.

$sqlacc="SELECT * FROM members INNER JOIN accounts ON 
account.acc_id=members.mem_id WHERE members.firstname='$username' and accounts.password='$password'";
$resultacc = mysql_query($sqlacc);
$countacc = mysql_num_rows($resultacc);
$rowacc = mysql_fetch_array($resultacc, MYSQL_NUM);

if($countacc==1){
$_SESSION['idontknow']=$rowacc[0];
$_SESSION['idontknow']=$rowacc[1];
header("location:content/index.php");
}
David Robinson
  • 77,383
  • 16
  • 167
  • 187

2 Answers2

0

Try below code

session_start();

$link = mysql_connect('localhost', 'mysql_username', 'mysql_password');

mysql_select_db('dbname', $link);

$username=mysql_real_escape_string($_POST['user']);        
$password=mysql_real_escape_string($_POST['pass']);

$sqlacc=" select * ".
        " from members m ".
        " inner join accounts a on a.mem_id=m.mem_id ".
        " where a.password='".$password."' and m.firstname='".$username."'";

$result = mysql_query($sqlacc);
$countacc = mysql_num_rows($result);

if($countacc==1){
    $rowacc = mysql_fetch_array($result);
    $_SESSION['memId']=$rowacc[0];
    $_SESSION['accId']=$rowacc[2];
    header("Location:content/index.php");
}

In your query the joining condition is wrong

account.acc_id=members.mem_id

Note: You can perform join on any two columns only the criteria is they must have same data-type and size.

Swapnil Patil
  • 971
  • 2
  • 18
  • 41
  • how about in this code after login.
    $username=mysql_real_escape_string($_POST['user']); $password=mysql_real_escape_string($_POST['pass']); $sqlacc="SELECT * FROM members INNER JOIN accounts ON account.mem_id =members.mem_id WHERE members.firstname='$username' and accounts.password='$password'"; $resultacc = mysql_query($sqlacc); $countacc = mysql_num_rows($resultacc); $rowacc = mysql_fetch_array($resultacc, MYSQL_NUM); if($countacc==1){ $_SESSION['______']=$rowacc[0]; $_SESSION['______']=$rowacc[1]; header("location: index.php"); }
    – Daniel Reyes Oct 01 '13 at 05:13
  • its not working. but i know your code is right. this my real problem [MYPROBLEM]http://stackoverflow.com/questions/19109081/not-yet-solve-login-username-firstname-in-table1-and-password-in-table2 – Daniel Reyes Oct 01 '13 at 06:05
  • @DanielReyes ,What is "my real problem".You have to use only session variable name their.Do you using session which is created by someone – Swapnil Patil Oct 01 '13 at 06:18
  • sorry if i am so disturbance.. hehehe. my real problem is in this link.. http://stackoverflow.com/questions/19109081/not-yet-solve-login-username-firstname-in-table1-and-password-in-table2 – Daniel Reyes Oct 01 '13 at 06:21
  • @DanielReyes checkout session settings http://support.qualityunit.com/021373-How-To-Enable-Session-Support-for-PHP – Swapnil Patil Oct 01 '13 at 06:21
0

Try This:

In your query join condition wrongly mapped (account.acc_id=members.mem_id)

SELECT * FROM members 
INNER JOIN account ON account.mem_id =members.mem_id 
WHERE members.firstname='$username' and accounts.password='$password'";


$sqlacc="SELECT * FROM members INNER JOIN accounts ON 
account.mem_id =members.mem_id WHERE members.firstname='$username' and accounts.password='$password'";
bgs
  • 3,061
  • 7
  • 40
  • 58
  • Is your condition correct account.mem_id =members.mem_id ? table name is accounts – Swapnil Patil Oct 01 '13 at 05:14
  • how about in this code after login.
    $username=mysql_real_escape_string($_POST['user']); $password=mysql_real_escape_string($_POST['pass']); $sqlacc="SELECT * FROM members INNER JOIN accounts ON account.mem_id =members.mem_id WHERE members.firstname='$username' and accounts.password='$password'"; $resultacc = mysql_query($sqlacc); $countacc = mysql_num_rows($resultacc); $rowacc = mysql_fetch_array($resultacc, MYSQL_NUM); if($countacc==1){ $_SESSION['______']=$rowacc[0]; $_SESSION['______']=$rowacc[1]; header("location: index.php"); }
    – Daniel Reyes Oct 01 '13 at 05:14
  • not yet. because some of my code is not right... code above that have error. if($countacc==1){ $rowacc = mysql_fetch_array($result); $_SESSION['idontknow']=$rowacc[0]; $_SESSION['idontknow']=$rowacc[1]; – Daniel Reyes Oct 01 '13 at 05:29