I am trying to login with email & Password inside form with help of below code, but i am getting : Fatal error: Using $this when not in object context in
Form
<form method="post">
<?php
if(isset($_GET['error']))
{
?>
<div>
<button data-dismiss='alert'>×</button>
<strong>Wrong Details!</strong>
</div>
<?php
}
?>
<input type="email" name="txtemail" />
<input type="password" name="txtpass" />
<button type="submit" name="btn-login">Sign in</button>
</form>
isset
if(isset($_POST['btn-login']))
{
try
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
$stmt->execute(array(":email_id" => $email));
$userRow = $stmt->fetch(PDO::FETCH_ASSOC);
$password = password_hash('upass', PASSWORD_DEFAULT);
if ( $stmt->rowCount() == 1 )
{
if ( $userRow[ 'userStatus' ] == "Y" )
{
if( password_verify( $_POST[ "upass" ] , $userRow[ 'userPass' ] ) )
{
if( password_needs_rehash( 'PASSWORD', PASSWORD_DEFAULT ) )
{
$new_pass = password_hash('upass', PASSWORD_DEFAULT);
// Update database
}
$_SESSION[ 'userSession' ] = $userRow[ 'userID' ];
return true;
}
else
{
header( "Location: index.php?error" );
exit;
}
}
else
{
header( "Location: index.php?inactive" );
exit;
}
}
else
{
header( "Location: index.php?error" );
exit;
}
}
catch ( PDOException $ex )
{
echo $ex->getMessage();
}
}
so i removed $this->
from line :
$stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
& replaced as
$stmt = conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
,
now i am getting Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
so i tried like : $conn->prepare
now i am facing : Fatal error: Call to a member function prepare() on a non-object in
so i seems i need to replace the old code to fix it, if so how to solve this error : Fatal error: Using $this when not in object context in
Updated question with db connection file :
<?php
global $db;
$db = isset($_POST['db']) ? $_POST['db'] : '';
class Database
{
private $host = "localhost";
private $db_name = "designer5";
private $username = "root";
private $password = "645644";
public $conn;
public function dbConnection()
{
global $db;
$db = isset($_POST['db']) ? $_POST['db'] : '';
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
Note : I researched lot of links in internet, but i didt found solution anywhere for my code :-(