Jump to content

Why I am getting error messages even though I have referred to 6 different websites and I see nothing wrong with my codes?


Go to solution Solved by ajoo,

Recommended Posts

I am trying to create a "login" webpage. The PHP codes for the login (login100.php) are called by another file (index100.php). Whenever I run index100.php on XAMPP, I get two errors: 

 

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\login100.php on line 24

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login100.php on line 26

 

My codes for index.php are:

 

<?php

include('login100.php'); // Includes Login Script

 

if(isset($_SESSION['login_user'])){

header("location: profile100.php");

}

?>

<!DOCTYPE html>

<html>

<head>

<title>Login Form in PHP with Session</title>

<link href="style100.css" rel="stylesheet" type="text/css">

</head>

<body>

<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>

 

My codes for login.php are:

 

<?php

session_start(); // Starting Session

$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 by passing server_name, user_id and password as a parameter

$connection = mysqli_connect("localhost", "root", "", "Company");

// To protect MySQL injection for Security purpose

$username = stripslashes($username);

$password = stripslashes($password);

$username = mysqli_real_escape_string($connection, $username);

$password = mysqli_real_escape_string($connection, $password);

// SQL query to fetch information of registerd users and finds user match.

$sql = "SELECT * FROM login where password='$password' AND username='$username'";

$query = mysqli_query($connection, $sql);

if (false === $query) {

    echo mysqli_error();

}

$rows = mysqli_num_rows($query);

if ($rows == 1) {

$_SESSION['login_user']=$username; // Initializing Session

header("location: profile100.php"); // Redirecting To Other Page

} else {

$error = "Username or Password is invalid";

}

mysqli_close($connection); // Closing Connection

}

}

?>

 

I have worked on this for the past FIVE days and I cannot resolve this issue. All I want is a code which fetches a user's username and password from the database, and no such username or password exist, a message saying "invalid password" is generated. For some reason, I'm getting the two error messages above. Can someone help?

  • Solution

Hi , try this :

 

Caution : I changed the database name and the field username to suit my own testing. It works and gives no errors

 

 

index100.php

<?php
include('login100.php'); // Includes Login Script
session_start(); 
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style100.css" rel="stylesheet" type="text/css">
</head>
<body>
<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>
 

login100.php

<?php
session_start(); // Starting Session
$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 by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "", "test");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
// SQL query to fetch information of registerd users and finds user match.
$sql = "SELECT * FROM users where password='$password' AND name='$username'";
$result = mysqli_query($connection, $sql);
if ($result === false) {
    echo mysqli_error($connection);
}
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: secure100.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
}
}
?>

secure100.php

 

<?php 
session_start();
echo " Welcome ".$_SESSION['login_user'];
echo " You are logged in.";

?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.