0

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'>&times;</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 :-(

  • Have you included **database connection** file there? – Nana Partykar Nov 04 '16 at 09:38
  • yes, included `require_once 'class.user.php';` –  Nov 04 '16 at 09:40
  • So, in class.user.php, database connection file is present? – Nana Partykar Nov 04 '16 at 09:40
  • in class.usr.php i included `require_once 'dbconfig.php';` i included dbconnection file in question..... –  Nov 04 '16 at 09:41
  • 2
    hi Your common sense , the question you linked has completely different code than my question, than how it will become duplicate ? –  Nov 04 '16 at 09:44
  • `require_once 'dbconfig.php'; $conn = new Database(); $stmt = $conn->dbConnection->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");` – Razib Al Mamun Nov 04 '16 at 09:50
  • @RazibAlMamun getting `Notice: Undefined property: Database::$dbConnection in ` & `Fatal error: Call to a member function prepare() on a non-object in ` –  Nov 04 '16 at 09:53
  • sorry, now try `$stmt = $conn->dbConnection()->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");` – Razib Al Mamun Nov 04 '16 at 09:54
  • @RazibAlMamun thank you so much buddy, now no errors, but when i try to login with correct `username & password` i am getting `Wrong Details!` –  Nov 04 '16 at 09:56
  • wrong details means? – Razib Al Mamun Nov 04 '16 at 09:57
  • @RazibAlMamun please check code i posted in question under `form` , i am using `password_default`[bcrypt] to save password , i done successfull registration, now i am trying to login with help of `password_verify` –  Nov 04 '16 at 10:00

0 Answers0