Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
session_start();
$error = "username/password incorrect";
$connection=mysqli_connect("localhost","root","","bblog");

//php cheacking To login
						if(isset($_POST["submit"])){
							$error=1;
							$message = "";
							if($_POST["username"]==""){
								$message = $message."Error:<br/> Please Enter User Name";
							}else {
								$error = 0;
							}
							if($_POST["password"]==""){
								$message = $message."<br/> Please Enter Password";
							}else {
								$error = 0;
							}
							if($_POST["user_type"]=="Select User Type"){
								$message = $message."<br/> Please select user type";
							}else {
								$error = 0;
							}
//database related

if(isset($_POST['submit'])){
	$username=$_POST['username'];
	$password=$_POST['password'];
	$user_type=$_POST['user_type'];
$sql= "SELECT * FROM users WHERE username='$username' and password='$password' and user_type='$user_type'";
// $stmt=$connection->prepare($query);
// $stmt->bind_param("sss",$username,$password,$user_type);
// $stmt->execute();
// $result=$stmt->get_result();
// $row=$result->fetch_assoc();
// session_regenerate_id();
// $_SESSION['username']=$row['username'];
// $_SESSION['user_type']=$row['user_type'];
// $_SESSION['password']=$row['password'];
// session_write_close();
// if ($result->num_rows==1 && $_SESSION['user_type']=="student") {
// 	header("location:student/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="Department") {
// 	header("location:Department/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="manager") {
// 	header("location:manager/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="Registrar") {
// 	header("location:Classes/home.php");
// }
$qr = mysqli_query($connection,$sql);
$result = mysqli_num_rows($qr);
								$row=mysqli_fetch_array($qr);
								if($result>0){
									$_SESSION["username"]=$_POST["username"];
									$_SESSION["user_type"]=$_POST["user_type"];
									$_SESSION["id"]=$row["user_id"];
									$_SESSION["dept"]=$row["department"];
									if($_SESSION["user_type"]=="manager" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: manager/home.php");
									}
									else if($_SESSION["user_type"]=="Department" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: Department/home.php");
									}
									else if($_SESSION["user_type"]=="student" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: student/home.php");
									}
									else if($_SESSION["user_type"]=="Registrar" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: Classes/home.php");
									}

}else{
								echo "<div class='error'>"."Error".$message."</div>";
							}
							}




else{
								echo "<div class='error'>"."Error".$message."</div>";
							}
}
?>


What I have tried:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\Cost Sharing\code.php on line 55
Posted
Updated 2-Aug-21 16:19pm

1 solution

If the query fails then the result from mysqli_query is a boolean (false). So first investigate if the query has failed, and if it hasn't, then check the number of rows. Consider the following example
PHP
...
if ($qr = mysqli_query($connection,$sql)) {
   $result = mysqli_num_rows($qr);
   ...

One likely reason for your query to fail is that you concatenate values directly to the SQL statement. This leaves you open to SQL injection and introduces different kinds of problems, see SQL injection - Wikipedia[^]

So try using parameters instead, for more information, see PHP: mysqli::prepare - Manual[^]

One more observation, you seem to store the password as plain text. This should never be done. To properly handle passwords, see Password Storage: How to do it.[^]
 
Share this answer
 
Comments
Ric W 14981652 5-Dec-23 14:45pm    
Thanks, I hadn't realised that the 'bool' refers to the case where the query might fail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900