lundi 25 mai 2020

I can't resolve this error Call to a member function quote() on null

I'm programming a website on localhost and I found this error when I try to login o submit a new user

Fatal error: Uncaught Error: Call to a member function quote() on null in C:\xampp\htdocs\recipes\db_manager\users.php:31 Stack trace: #0 C:\xampp\htdocs\recipes\users_manager\checkSubmit.php(15): notExistsUser('usertest') #1 {main} thrown in C:\xampp\htdocs\recipes\db_manager\users.php on line 31

checkSubmit.php:

<?php
    session_start();
    include_once("../db_manager/common.php");
    if(isset($_POST["username"]) && isset($_POST["pwd"])) {
        $username = $_POST["username"];
        $pwd = $_POST["pwd"];
        if(notExistsUser($username)){
            submit($username, $pwd);
            redirect("../index.php", "User successfully submitted!");
        } else {
            redirect("../submit.php", "This username already exists");
        }
    }
?>

funcion notExistsUser in users.php

function notExistsUser($username) {
    $db = attachdb();
    $user = $db->quote($username); //Line 31
    $rows = $db->query("SELECT * FROM Users WHERE username = $user");
    if($rows->rowCount() > 0)
        return false;
    else
        return true;
}

function attachdb()

function attachdb() {
    $add = 'mysql:dbname=Recipes;host=localhost';
    try {
        $db = new PDO($add, 'root', 'mysql');
        return $db;
    } catch (PDOException $ex) {
        ?>
        <p>A database error occurred!.</p>
        <?php
        return NULL;
    }
}

What this error means and how can I fix it?




Aucun commentaire:

Enregistrer un commentaire