Jump to content

Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\thato\index.php:31 Stack trace: #0 {main} thrown in C:\xampp\htdocs\thato\index.php on line 31


appleorange

Recommended Posts

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vuyanii_lesegoj";
$conn = null;
//$dbname = "temp_StoreDB";
$conn = null;
global $bcrypt; 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Connected to DB successfully <br>";
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

if(isset($_POST['login'])){
            $username   = $_POST['username'];
            $password   = $_POST['password'];
            $message    = '';
     
         $query = "SELECT count() FROM `users` WHERE `username`= ? AND `password` = ?";
        
         $stmt = $conn->prepare($query); //line 31
         $stmt->execute([$username,$password]);
        
         $stmtl = $stmt->fetchColumn();

         if($stmtl == 1){
            $_SESSION['username'] = $username;

            $dated = date('y/m/d');
            $ip    = $_SERVER['REMOTE_ADDR']; 
           try{
                   $query = "INSERT INTO visitors(user_id,username,dated,ip) 
                VALUES(:fn, :ln, :em, :con)";
            $stmt = $conn->prepare($query);
           $stmt->bindparam(":fn",$user_id);
            $stmt->bindparam(":ln",$username);
            $stmt->bindparam(":em",$dated);
            $stmt->bindparam(":con",$ip);
           
            
            $stmt->execute();

            header('location:welcome.php');
           }
                       catch(PDOException $e){
                echo $e->getMessage();
            }
             echo $dated;

             $message ='<p style="color:red;">Logged In successfully</p>';      
         }else{
             
            $message ='<p style="color:red;">Incorrect Details Entered</p>';  
            //echo "$message";           
         }
}
?>

Link to comment
Share on other sites

XcQfW3F.png

Try using the code block system on the forums, so that your code doesn't get lost in the formatting.

 

The error in the title would only come about if your connection errors out.

What is the line of output before the title?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...