Hey Ieders,

Ik ben bezig met een OOP script, echter krijg ik de volgende melding

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Insert::__construct(), 0 passed in C:\xampp\htdocs\INSERT\index.php on line 57 and exactly 1 expected in C:\xampp\htdocs\INSERT\core.php:13 Stack trace: #0 C:\xampp\htdocs\INSERT\index.php(57): Insert->__construct() #1 {main} thrown in C:\xampp\htdocs\INSERT\core.php on line 13

Op het moment van schrijven heb ik drie bestanden aangemaakt.

- Core (bevat de insert class)
- databaseconnect.php (bevat connectie naar MariaDB)
- Index.php (roept insert classe aan)




Core.php
<?php
include 'databaseconnect.php';

class Insert

{
protected $dbConnection;
protected $title;
protected $Publisher;
protected $Genre;
protected $VisibleInStore;

public function __construct(PDO $dbConnection)
{
$this->dbCOnnection = $dbConnection;
}

public function Add($title, $Publisher, $Genre, $VisibleInStore)
{


$stmt = $this->dbConnection->prepare('INSERT INTO `game` ( `Title`, `Publisher`, `Genre`, `VisibleInStore`) VALUES ("$title" , "$Publisher", "$Genre", "$VisibleInStore")');
return exec($stmt);


$conn = null;
}
}
?>

databaseconnection.php


<?php
$dbConnection = new PDO('mysql:host=localhost;dbname=gameplanet', 'root', '');
?>

index.php

<?php
include 'core.php';
$insert = new Insert();
$insert->Add('title', 'Publisher', 'Genre', 'VisibleInStore')




?>
Kijk eens naar regel 26, deze klopt sowieso al niet, meer informatie over de exec()-functie.

De foutmelding krijg je omdat je in index.php de Insert-class aanroept zonder een PDO-object mee te geven, dit geef je op regel 13 van Core.php aan.


Misschien ga je iets te hard van stapel?



Edit:
Het volgende stukje van regel 26 zal ook niet werken.

$conn = null;

De variable $conn bestaat namelijk niet in je method.

Reageren