Hi I made a login for a website.
I made a successful one using 1 account table in a database.
Now I made another table for another account. Both tables are different in attributes that is why I separated them.
I am having a hard time log-in the second one in by using this algorithim.
The inputted login and password come from another page using a HTML Form.
<?php
//Start session
session_start();
//Include database connection details
require_once("config.php");
require("clean.php");
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
$errmsg_arr[] = 'Login ID missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login-form.php");
exit();
}
//Create query for patient
$qry="SELECT * FROM user_info WHERE login='$login' AND password='".md5($password)."'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful User
session_regenerate_id();
$user_info = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $user_info['ID_NO'];
$_SESSION['SESS_FIRST_NAME'] = $user_info['FNAME'];
$_SESSION['SESS_admin'] = $user_info['admin'];
$_SESSION['SESS_itResult'] = $user_info['itResult'];
//print "Login a success!";
session_write_close();
header("location: member-index.php");
exit();
}
else {
//Create query for
$qry="SELECT * FROM specialist_info WHERE spec_username='$login' AND spec_password='".md5($password)."'";
$result2=mysql_query($qry);
//Check whether the query was successful or not
if($result2) {
if(mysql_num_rows($result2) == 1) {
//Login Successful User
session_regenerate_id();
$specialist_info = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $specialist_info['spec_id'];
$_SESSION['SESS_FIRST_NAME'] = $specialist_info['name'];
$_SESSION['SESS_admin'] = 1;
//print "Login a success!";
session_write_close();
header("location: member-index.php");
exit();
}
else {
//Login failed
header("location: login-failed.php");
exit();
}
}
}
}
else {
die("Query failed");
}
}
?>
I was able to log-in the first(meaning there is connection between PHP and MySQL) account however I could not in the second.
From what i follow, I could not get in this one. “if(mysql_num_rows($result2) == 1)”
I know there is data in the second table in the database because i checked it(there is MD5 yes).
Maybe my logic is wrong. Suggestions? 😀 Thanks!
I’m not sure about the code (will review later) but you could try refactoring your tables so you have a separate table that contains user attributes, this allows you to be dynamic with your attributes so you don’t have to create a different table every time you have a different type of user.
EDIT:
Try:
var_dump(mysql_num_rows($result2))if this shows the return value is false something is wrong with the query, otherwise it doesn’t match any rows.