M
M
mihailos2020-04-19 09:19:56
PHP
mihailos, 2020-04-19 09:19:56

Why is he cursing?

I recently started to understand OOP and immediately ran into the problem
Fatal error: Uncaught Error: Call to undefined function add_in_db() in E:\OSPanel\domains\tutorial\index.php:7 Stack trace: #0 {main} thrown in E: \OSPanel\domains\tutorial\index.php on line 7

Here is the index.php code

<?php
spl_autoload_register(function($className){
    require_once __DIR__ . '/src//' . $className . '.php';
});

$add = new Project\Model\Task\BD();
var_dump(add_in_db());

Here is the BD.php class file itself
<?php
namespace Project\Model\Task;

class BD{
    public $pdo;
    public function add_in_db($task, $table)
    {
        $this->pdo = new PDO('mysql:host=localhost; dbname=listask', 'root', '');
        $query = $pdo->prepare("INSERT INTO $table(task) VALUES(?)");
        $query->execute([$task]);
    }
    public function delete_from_db($id)
    {
        $query = $pdo->prepare("DELETE FROM tasks WHERE id = ? ");
        $query->execute([$id]);
    }
}

What is the problem? And how to solve it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2020-04-19
@phaggi

An error means that the constant contains an invalid operation. Apparently, we are talking about PD0. Are you sure you need a constant? Then why are you passing the result of some calculations with parameters to it? Maybe it shouldn't be a constant?

V
volodec, 2020-04-19
@volodec

var_dump($add->add_in_db());
But the error will be in any case, because with such a method call, the required parameters will not be passed, in particular $task, $table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question