0

I started over with a project I was working on earlier. With the help of my PHP book and a few people on Stack Overflow, I was able to get rid of a lot of the errors I was getting.

Now, I am left with a warning and a fatal error. I understand both errors, I am just not sure how to fix the errors I am getting. Could anyone please help? I am not asking for anyone to write the code or just do everything for me, I just need a point in the right direction.

This is the warning, and it sounds like I need to put __construct somewhere in the uploadFile() method, but again, not sure about this:

Warning: PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: No error: PDO constructor was not called in C:\xampp\htdocs\files\upload.php on line 23

The second, I am just completely lost on what to do about. I realize what the error is telling me, I just don't know where to start in terms of fixing it. Here it is:

Fatal error: Call to undefined method upload::bindParam() in C:\xampp\htdocs\files\upload.php on line 24

If there's anymore information I could provide that would make this easier to answer, please tell me and I will try to get it for you. I appreciate any help!

upload.php

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

class upload extends PDO
{
    public $conURL, $filename, $tmpname, $filesize, $filetype, $file;

    public function __construct($config) {
        $conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
        parent::__construct($conURL, $config['user'], $config['pass']);

    }       

    public function uploadFile() {
        if ($_FILES['file']['size'] >= 2000000) {
           echo "File is too large!"; 
        }
        else {
            $sth = $this->prepare("INSERT INTO uploads (name, type, size, file) VALUES (?, ?, ?, ?)");
            $sth->bindParam(1, $filename);
            $sth->bindParam(2, $filetype);
            $sth->bindParam(3, $filesize);
            $sth->bindParam(4, $file);
            $sth->execute();
        }
    }
}

$upload = new upload($config);

$upload->filename = $_FILES['file']['name'];
$upload->tmpname = $_FILES['file']['tmp_name'];
$upload->filesize = $_FILES['file']['size'];
$upload->filetype = $_FILES['file']['type'];    
$upload->file = $_FILES['file'];


$upload->uploadFile();

config.php

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

Edit: Thanks to everyone here, only one error remains and it is the following:

Fatal error: Call to undefined method stdClass::uploadFile() in C:\xampp\htdocs\files\upload.php on line 43

Edit 2: All errors have been resolved, but nothing is being inserted into the database.

  • You should use `execute()` and `bindParam()` on a `PDOStatement` object and not the actual `PDO` object, the `prepare()` returns such an object. – Cyclonecode Jul 19 '12 at 04:05
  • In addition to @KristerAndersson - `class upload extends PDO` - is a great example that you always should prefer composition over inheritance – zerkms Jul 19 '12 at 04:07

3 Answers3

1

well 2 guys pointed another issue, but really point here is you are calling bindParam of PDO subclass. That needs to be pdostatement instaed. try:

        $sth = $this->prepare("INSERT INTO uploads (name, type, size, file) VALUES (?, ?, ?, ?)");
        $sth->bindParam(1, $this->filename);
        $sth->bindParam(2, $this->filetype);
        $sth->bindParam(3, $this->filesize);
        $sth->bindParam(4, $this->file);
        $sth->execute();

Of course you need to fix following errors in your constructor as well.

EDIT:

I guess the constructer is failing and throwing exception. Because $this->conURL is null. Change

$conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];

to

$this->conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];

Also I changed my previous answer, please see bindparam()'s 2nd parameters.

mask8
  • 3,347
  • 21
  • 32
  • Also, OP uses undefined variables, instead of object properties. I'm wondering if there is a single line without errors )) – zerkms Jul 19 '12 at 04:18
0

Your constructor should look like:

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();
        }
}  

or even

public function __construct($config)
{
    $conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
    parent::__construct($conURL, $config['user'], $config['pass']);
}  
zerkms
  • 230,357
  • 57
  • 408
  • 498
  • Thanks! Although, when I replace "return new PDO" with "parent::__construct," it seems to break my uploadFile() method. It's now saying the method is undefined on line 43 (last line). What am I missing here? –  Jul 19 '12 at 04:08
  • @Dustin L.: see comments and another answers. You're calling the method `PDO` doesn't have – zerkms Jul 19 '12 at 04:09
  • This is the error I am now getting: Fatal error: Call to undefined method stdClass::uploadFile() in C:\xampp\htdocs\files\upload.php on line 43 –  Jul 19 '12 at 04:12
  • @Dustin L.: put 40-45 lines of that file to the question, we cannot guess (append your question with new file version and mark correspondent lines) – zerkms Jul 19 '12 at 04:15
  • All the code that is in upload.php is posted. It ends on line 43. –  Jul 19 '12 at 04:16
  • @Dustin L.: I see the update and I would say that it's not possible that the code throws `Call to undefined method stdClass::uploadFile()` error. It is just impossible. There is a chance you're requesting or modifying a wrong file. – zerkms Jul 19 '12 at 04:20
  • Lol, thank you, but it is indeed throwing that. All the code is pasted except for the HTML form. –  Jul 19 '12 at 04:24
  • @Dustin L.: it's not possible. There is no `stdClass` object there. So double check that you're requesting and/or modifying the correct file, and that your browser didn't cache response – zerkms Jul 19 '12 at 04:25
  • Well, I found this if it helps: http://stackoverflow.com/questions/931407/what-is-stdclass-in-php –  Jul 19 '12 at 04:28
  • @Dustin L.: I know what `stdClass` is. And I don't see it in your code, I only see `upload` class – zerkms Jul 19 '12 at 04:30
  • @Dustin L.: check that you're modifying and requesting correct file. The code you've shown **cannot** throw such error – zerkms Jul 19 '12 at 04:34
  • @Dustin L.: why did you comment out `error_reporting`? It's the most important line of all your file – zerkms Jul 19 '12 at 04:37
  • @Dustin L.: put `var_dump($upload);` on the 35th and 42th lines (yes, twice) and show output here – zerkms Jul 19 '12 at 04:38
  • I was testing something. When it's commented out, I get another error. The two may or may not be related: Strict Standards: Creating default object from empty value in C:\xampp\htdocs\files\upload.php on line 36 –  Jul 19 '12 at 04:39
  • I think the constructor is failing and returning stdclass. please see my answer – mask8 Jul 19 '12 at 04:40
  • @Dustin L.: **IT IS IMPORTANT**. Omg, why the hell do you program in that way?! Do you drive with your eyes closed? :-S (sorry, it just confuses me a lot) – zerkms Jul 19 '12 at 04:40
  • @mask8: Even if it fails - the object should be of `upload` class – zerkms Jul 19 '12 at 04:41
  • @Dustin L.: replace your constructor with the 2nd one from my answer, and check for the second time that in my answer there is no `$this->` for `$conUrl`. Wondering why there was no notice for that :-S Or was it? – zerkms Jul 19 '12 at 04:42
  • var_dump returned null. Zerk, I put it back...I was just testing... I am no longer getting any errors, but nothing is being inserted into the database. –  Jul 19 '12 at 04:44
  • Update your question with the latest file version – zerkms Jul 19 '12 at 04:45
  • @Dustin L.: so - there is `object(upload)`, not `stdClass` – zerkms Jul 19 '12 at 04:56
  • The error I got was stdClass. Anyway, nothing is being inserted into the database. –  Jul 19 '12 at 04:57
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14109/discussion-between-zerkms-and-dustin-l) – zerkms Jul 19 '12 at 04:59
0

Because you are extending PDO, so

return new PDO($this->conURL, $config['user'], $config['pass']);

Should be

parent::__construct($this->conURL, $config['user'], $config['pass']);
xdazz
  • 149,740
  • 33
  • 229
  • 258