Hello Everyone,
This is my first post and im sure this community can help.

Issue:I'm trying to create an authentication script for the first time, I am inserting Data into a table and trying to retrieve it again on the same page but instead of retrieving the latest id it is retrieving the second last ID you will see my code below, Is their a work around for this or am I doing it all wrong? Hopefully there is some way of doing it.

$username = mysql_real_escape_string($_POST['userName']);
$password = mysql_real_escape_string($_POST['password']);
$sqlCheckUser="SELECT * FROM users WHERE userName='$username' AND password='$password' AND rights != 0";
$sqlCheckUser_results=mysql_query($sqlCheckUser);



$countUsers = mysql_num_rows($sqlCheckUser_results);
if($countUsers==1){
	$rowUsers = mysql_fetch_array($sqlCheckUser_results);
	$userID = $rowUsers['id'];
	$userName = $rowUsers['userName'];
	$loginCode = rand(1000000000, 9000000000);

	mysql_query("INSERT INTO usersLoginHistory (userId,loginCode) VALUES ('$userID','$loginCode')");
	

	$sqlGetLoginCode="SELECT lastLogin FROM usersLoginHistory WHERE userId=$userID ORDER BY id DESC LIMIT 1";
	$sqlGetLoginCode_results=mysql_query($sqlGetLoginCode);
	$rowGetLoginCode = mysql_fetch_array($sqlGetLoginCode_results);
	$loginCode = $rowGetLoginCode['loginCode'];

	


	setcookie('username', $userName, time() + (86400 * 7));
	setcookie('logincode', $loginCode, time() + (86400 * 7));

	echo $_COOKIE['logincode'];
	
}
else {
	echo "The Server is not Serving?";
}

Recommended Answers

All 2 Replies

try this query

$sqlGetLoginCode="SELECT lastLogin 
FROM usersLoginHistory 
WHERE userId=".mysql_insert_id()."
ORDER BY id DESC LIMIT 1";

try this query

$sqlGetLoginCode="SELECT lastLogin 
FROM usersLoginHistory 
WHERE userId=".mysql_insert_id()."
ORDER BY id DESC LIMIT 1";

Unfortunately the above did not work for me so i have just worked around it and will verify the login code on the next page when a user tries to access an area that has rights attached to it.

$username = mysql_real_escape_string($_POST['userName']);
$password = mysql_real_escape_string($_POST['password']);
$sqlCheckUser="SELECT * FROM users WHERE userName='$username' AND password='$password' AND rights != 0";
$sqlCheckUser_results=mysql_query($sqlCheckUser);



$countUsers = mysql_num_rows($sqlCheckUser_results);
if($countUsers==1){
	$rowUsers = mysql_fetch_array($sqlCheckUser_results);
	$userID = $rowUsers['id'];
	$userName = $rowUsers['userName'];
	$loginCode = rand(1000000000, 9000000000);

	mysql_query("INSERT INTO usersLoginHistory (userId,loginCode) VALUES ('$userID','$loginCode')");
	

	setcookie('username', $userName, time() + (86400 * 7));
	setcookie('logincode', $loginCode, time() + (86400 * 7));

	echo $_COOKIE['logincode'];

}
else {
	echo "The Server is not Serving?";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.