For the past two days, I have been working on a project, and no matter what I try, I get an error of some sort, or something just doesn’t work. At this point, I am being told that I’m calling an undefined method, upload:prepare(), which is true because the class is called upload, and the statement is $this->prepare()….etc etc.
I am so frustrated that no matter what I do, I cannot figure this out.
classes.php
<?php
require('config.php');
error_reporting(E_ALL);
/*
* Classes required by the script
*
*/
class database extends PDO
{
public $conURL;
public function __construct($config) {
$conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
try {
parent::__construct($conURL, $config['user'], $config['pass']);
} catch (PDOException $e) {
$e->getMessage();
}
}
}
class upload
{
public $_FILES;
public function uploadFile() {
if ($_FILES['file']['size'] >= 2000000) {
echo "File is too large!";
}
elseif (isset($_FILES['file'])) {
$stmt = $this->prepare("INSERT INTO upload (name, type, size, content) VALUES (?, ?, ?, ?)");
$stmt->execute(array($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $_FILES['file']['file']));
}
}
}
config.php
<?php
$config = array(
'host' => 'localhost', // db host
'user' => 'root', // db user
'pass' => 'mypassword', //db pass
'db' => 'files' // db name
);
upload.php
<?php
error_reporting(E_ALL);
require('config.php');
require('classes.php');
$dbh = new database($config);
$upload = new upload();
$upload->uploadFile();
You’re attempting to call the
preparefunction ofupload, rather thandatabase. Try this: