0

在过去的两天里,我一直在做一个项目,无论我尝试什么,我都会遇到某种错误,或者某些东西不起作用。在这一点上,我被告知我正在调用一个未定义的方法,upload:prepare(),这是真的,因为该类被称为上传,并且语句是 $this->prepare()....等等.

我很沮丧,无论我做什么,我都无法弄清楚这一点。

类.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']));
        }
    }
}

配置文件

<?php
$config = array(
        'host' => 'localhost', // db host
        'user' => 'root', // db user
        'pass' => 'mypassword', //db pass
        'db' => 'files' // db name
);

上传.php

<?php
error_reporting(E_ALL);
require('config.php');
require('classes.php');

$dbh = new database($config);
$upload = new upload();
$upload->uploadFile();
4

2 回答 2

0

您正在尝试调用prepare,upload而不是database. 试试这个:

class upload
{
    public $_FILES;

    public function uploadFile($config) {
        $db = new database($config);
        if ($_FILES['file']['size'] >= 2000000) {
           echo "File is too large!";
        }
        elseif (isset($_FILES['file'])) {
           $stmt = $db->prepare("INSERT INTO upload (name, type, size, content) VALUES (?, ?, ?, ?)");
           $stmt->execute(array($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $_FILES['file']['file']));
        }
    }
}
于 2012-07-20T03:19:25.657 回答
0

我想你的意思是使用database->prepare而不是this->prepare

您需要让您的上传类成为数据库实例(或动态创建它)。

于 2012-07-20T03:20:37.400 回答