I am trying to run a SELECT query to validate attempted logins to a website. I am running the following code. My problem is that
either the query is returning 0 rows or them number of rows is not being captured. I have successfully executed the query in SSMS. I have verified it is being executed in my code by a if($stmt). I know the userid and password I am entering are valid.
Page that captures the userid and password:
?php
include('login_process.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body style="background-color:blue">
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>
</html>
Page that processes the SELECT query:
<?php
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server
$serverName = ("JOHN-PC\SQLEXPRESS");
$connectionInfo = array( "Database" => "kc3061users");
$conn = sqlsrv_connect($serverName, $connectionInfo);
// SQL query to fetch information of registerd users and finds user match.
$tsql = "SELECT userid, password FROM kc3061ligin where userid = '$userid' AND password = '$password'";
$stmt = sqlsrv_query($conn,$tsql);
$row_count = sqlsrv_num_rows($stmt);
if ($row_count === true){
echo "row count is ";
echo $row_count;
header("location: ../secure_page.html");
}
else {
$error = "Username or Password is invalid";
}
sqlsrv_close($conn); // Closing Connection
}
}
?>
John Scholl