Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am not master with PHP, but what i am trying to do is creating login form and i am getting this error message:

Warning: Undefined variable $conn in C:\xampp\htdocs\BeravaPhp\phpfiles\login_ini.php on line 10

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\BeravaPhp\phpfiles\login_ini.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\BeravaPhp\phpfiles\login_ini.php on line 10


the code in login_ini.php is:

PHP
<pre><?php
include "dbconn.php";

if (isset($_POST['submit'])) {
    try {
        $cuid = $_POST['username'];
        $cpass = $_POST['password'];

        // Check if username exists in Clients table
        $stmt = $conn->prepare("SELECT * FROM Clients WHERE CUID = :cuid");
        $stmt->execute(array(':cuid' => $cuid));
        $result = $stmt->fetch();

        if ($result) {
            if (password_verify($cpass, $result['CPass'])) {
                header("Location: dashboard.html");
                exit;
            } else {
                echo "Invalid password";
            }
        } else {
            // Check if username exists in SubUser table
            $stmt = $conn->prepare("SELECT * FROM SubUser WHERE SubUsrID = :cuid");
            $stmt->execute(array(':cuid' => $cuid));
            $result = $stmt->fetch();

            if ($result) {
                if (password_verify($cpass, $result['SubUsrPass1'])) {
                    header("Location: dashboard-Sub.html");
                    exit;
                } else {
                    echo "Invalid password";
                }
            } else {
                // Check if username exists in BervEmp table
                $stmt = $conn->prepare("SELECT * FROM BervEmp WHERE BerEmpID = :cuid");
                $stmt->execute(array(':cuid' => $cuid));
                $result = $stmt->fetch();

                if ($result) {
                    if (password_verify($cpass, $result['pass'])) {
                        header("Location: empdashboard.html");
                        exit;
                    } else {
                        echo "Invalid password";
                    }
                } else {
                    echo "Invalid username";
                }
            }
        }
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>


and the code inside dbconn.php is:

PHP
<pre>dsn = $dsn;
        $this->user = $user;
        $this->password = $password;
    }

    private function connect()
    {
        try {
            $this->pdo = new PDO($this->dsn, $this->user, $this->password);
        } catch (PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }
    }

    public function getPdo()
    {
        if (!$this->pdo) {
            $this->connect();
        }
        return $this->pdo;
    }
}

$dsn = "pgsql:host=localhost;dbname=Beravaa";
$user = "postgres";
$password = "222333";

$db = new Db($dsn, $user, $password);
$pdo = $db->getPdo();

if ($pdo) {
    echo "Connected to the database successfully";
} else {
    echo "Failed to connect to the database";
}
?>


What I have tried:

So could someone advise what the error is and how to fix it?
Posted
Updated 14-Feb-23 0:20am
v2

The error is self explanatory: the variable $conn does not exist.
The code you show only uses $conn - it does not at any point declare it and assign it a value:
PHP
$stmt = $conn->prepare("SELECT * FROM Clients WHERE CUID = :cuid");
PHP
$stmt = $conn->prepare("SELECT * FROM SubUser WHERE SubUsrID = :cuid");
PHP
$stmt = $conn->prepare("SELECT * FROM BervEmp WHERE BerEmpID = :cuid");
Those are the only references to it at all ...
 
Share this answer
 
$conn has never been declared, it should be $pdo, which has been declared, as per your function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900