-1

I'm beginner in php and I have a problem that I can not explain by myself.. I have two files: the first one index.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
        <input name="file" type="file"/>
        <input type="submit" value="Wyslij" />
    </form>
</body>
</html>

and the second one upload.php:

<?php

if(isset($_FILES['file'])){
$db=new mysqli('localhost','root','kursphp','products');

    $name= $db->real_escape_string($_FILES['file']['name']);
    $type=$db->real_escape_string($_FILES['file']['type']);
    $data=$db->real_escape_string(file_get_contents($_FILES['file']['tmp_name']));
    $size=intval($_FILES['file']['size']);

    //question

    $query= "INSERT INFO files(name,type,size,data,created) VALUES ('{$name}','{$type}','{$size}','{$data}',NOW())";

    $result=$db->query($query);

    if($result){
    echo "File saved";
    } else {
        echo $db->error();
    }

    $db->close();
}

?>

When I upload some file, I get an error:

Fatal error: Call to undefined method mysqli::error() in D:\wamp\www\php_z\upload\upload.php on line 20

I don't know why I get this kind of error because as I declared: db is an object of mysqli. I read in documentation of PHP that

mysqli::$error -- mysqli_error — Returns a string description of the last error

So there is method such error().. Can someone tell me how to fix this?

SW4
  • 69,876
  • 20
  • 132
  • 137
user2534454
  • 385
  • 3
  • 13
  • 1
    `INSERT INFO files` you sure about that? Coding on an iPhone? lol typo right? They always are. – Funk Forty Niner Jan 06 '15 at 00:17
  • omg i made a mistake INSERT INTO files, now it works correctly. I'm coding in VS using free extension of PHP for VS 2013 its very bad i must say .. cuz of that i make this kind of stupid mistakes.. btw thx!! – user2534454 Jan 06 '15 at 00:35
  • You're welcome. So, problem solved I hope? Would you like me to post an answer for it? – Funk Forty Niner Jan 06 '15 at 00:36

3 Answers3

2

You've a few errors.

The first being:

INSERT INFO files
         ^

which should read as INTO and not INFO. The T and F are close to each other on the keyboard, so that can happen.

Then there's echo $db->error(); which should read as echo $db->error;

This should be an object oriented style and not a procedural style.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

It isn't a function, its a variable. Access it like this:

echo $db->error;

Read More

Darren
  • 13,050
  • 4
  • 41
  • 79
0

There is not a method named error. There is a variable named error. Thus, change

echo $db->error();

to

echo $db->error;

RhapX
  • 1,663
  • 2
  • 10
  • 17