At the very bottom I have posted what is visible on the webpage. Not sure what I'm doing, I will post other .php files that are linked to this if necessary. The webpage is also unusable, clicking login will do nothing except refresh the page. Not sure what changed but it was working before adding a few lines of code. I had trouble with new on login.php which accesses my database with connect.php
<?php
session_start();
include("classes/connect.php");
include("classes/login.php");
$email = "";
$password = "";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$login = new Login();
$result = $login->evaluate($_POST);
if($result != "")
{
echo "<div style='text-align:center;font-size:12px;color:white;background-color:grey;'>";
echo "<br>The following errors occured:<br><br>";
echo $result;
echo "</div>";
}else
{
header("Location: profile.php");
die;
}
$email = $_POST['email'];
$password = $_POST['password'];
}
?>
This is what is displayed at the top of the webpage
'''
evaluate($_POST);
if($result != "")
{
echo "
else
{
header("Location: profile.php");
die;
}
$email = $_POST['email'];
$password = $_POST['password'];
}
?>
'''
My login.php class starting throwing errors due to changing
'''
$DB = new Database();
$result = DB->read($query);
'''
to
'''
$DB = Database();
$result = (new db)->read($query);
'''
Here is the login.php class I have stored in my classes folder
'''
<?php
class Login
{
private $error = "";
public function evaluate($data)
{
$email = addsLashes($data['email']);
$password = addsLashes($data['password']);
$query = "select * from users where email = '$email' limit 1 ";
$DB = Database();
$result = (new db)->read($query);
if($result)
{
$row = $result[0];
if($password == $row['password'])
{
//create session data
$_SESSION['site_userid'] = $row['userid'];
}else
{
$error .= "wrong password<br>";
}
}else
{
$error .= "No such email was found<br>";
}
return $error;
}
}
'''
This is able to connect to the database using the code from connect.php where I created the Database class
'''
<?php
class Database
{
private $host = "localhost";
private $username = "root";
private $password = "root";
private $db = "site_db";
function connect()
{
$connection = mysqli_connect($this->host,$this->username,$this->password,$this->db);
return $connection;
}
function read($query)
{
$conn = $this->connect();
$result = mysqli_query($conn,$query);
if(!$result)
{
return false;
}
else
{
$data = false;
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
return $data;
}
}
function save($query)
{
$conn = $this->connect();
$result = mysqli_query($conn,$query);
if(!$result)
{
return false;
}else
{
return true;
}
}
}
?>
'''
I really think changing the new function messed everything up. I am following a tutorial which did not use (new db) and just new Database. Using new Database will throw a fatal error.
My Answer:
It looks like the issue may be with how you are instantiating the Database class in your login.php file. Instead of using `(new db)->read($query)`, you should use `$DB = new Database(); $result = $DB->read($query);`.
Try updating your login.php file to reflect this change and see if that resolves the issue with your login functionality. Let me know if you need further assistance.
Rate this post
4 of 5 based on 1346 votesComments