0

Hello I've made a quick code to insert name into a database.

This is the class for database connection:

(I hide the passwords etc, basically i define them in the top)

class Database
{
    private $pdo;
    private $query;
    public function __construct()
    {
        try
        {
            $this->pdo = new PDO('mysql:host='.MYSQL_HOST.';dbname=oop', MYSQL_USER, MYSQL_PASSWORD);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Sucessfully connected to MySQL server!';
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function query($name)
    {
        $this->query = $this->pdo->prepare("INSERT INTO user (name) VALUES (:name)");
        $this->query->bindValue(":name", $name);
        $this->query->execute();
        echo 'Sucessfully send!';
    }
}

This is the class that I basically use the queries and process them from :

class System
{
    private $Database;

    public function __construct(Database $Database = null)
    {
        $this->Database = $Database;
    }

    public function insertName($name)
    {
        if ($this->db != null)
        {
            $this->db->query($name);
        }
    }
}

This is config.php

include("system.class.php");
include("db.class.php");

$system = new System($Database);
$db = new Database();

And this is index.php

<?php

include("config.php");

?>

<html>
<form action="" method="post">
<input type="text" name="name">
<input type="submit">
</form>

<?php

    if (isset($_POST) && !empty($_POST['name']))
    {
        $Database->insertName($_POST['name']);
        echo 'Scessfuly inserted!';
    }
?>
</html>

Problem:

When sending the data (after submiting the form) I get the following error:

Fatal error: Call to a member function insertName() on a non-object in C:\xampp\htdocs\oop\index.php on line 17

What did I do wrong? This is line 17:

    $Database->insertName($_POST['name']);

How can I fix this and what is the problem?

Thnaks.

After changing it from $db to the actual name of the class I get this:

Call to undefined method Database::insertName() in C:\xampp\htdocs\oop\index.php on line 17

EDIT:

if (isset($_POST) && !empty($_POST['name']))
{
    $system->insertName($_POST['name']);
    echo 'Scessfuly inserted!';
}


class System
{
    private $Database;

    public function __construct(Database $Database = null)
    {
        $this->Database = $Database;
    }

    public function insertName($name)
    {
        if ($this->Database != null)
        {
            $this->Database->query($name);
        }
    }
}
user2286576
  • 31
  • 1
  • 7

2 Answers2

2

It looks like you have two issues. The first being, you are trying to call a method on a variable that is not defined. In config.php you define the Database object as $db, not $Database. Secondly, the method insertName() is defined in the class System not Database.

Try changing the line to $system->insertName($_POST['name'])

Zane M.
  • 490
  • 2
  • 7
1

I quote:

$db = new Database();

Why do you use $Database when you called the variable $db?

Use:

$db->insertName($_POST['name']);

After TO has edited question:

The method insertName is in the System class. So use $system->insertName(...);.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • Just noticed that.. But updated the question – user2286576 Apr 17 '13 at 17:29
  • I now got rid of all errors, but it doesn't seem to insert anything to the database!. Any idea why? – user2286576 Apr 17 '13 at 17:33
  • @user2286576 `$system = new System($Database); $db = new Database();` There is now the error. Change it to `$db = new Database(); $system = new System($db);` and it'll work. – bwoebi Apr 17 '13 at 17:34
  • I've already changed the other one, I get no errors now, but the content won't get inserted for some reason. No error at all. is there a way to check why? – user2286576 Apr 17 '13 at 17:36
  • @user2286576 Did you already change the order (not only the variables)? If yes try adding `error_reporting(-1); ini_set("display_errors", 1);` and check if it reports some error. – bwoebi Apr 17 '13 at 17:39