2

I have problem with adding image into database, it's insert all fill just image get error and it's not added image. Here is code:

<?php 
include '../dbc.php'; 

$message='';
if(isset($_POST["submit"])){
  $name = $_POST["name"];
  $descr = $_POST["descr"];
  $price = $_POST["price"];
  $quantity = $_POST["quantity"];
  $item_number = $_POST["item_number"];
  $cat = $_POST["cat"];
  $file = $_FILES['image']['tmp_name']; 


 if((!$name) || (!$desc) || (!$price) || (!$quantity) || (!$item_number) || 
    (!$cat) || (!$image)){
    $message = "Fill all inputs!";
  }
  $name_query = mysql_query("SELECT name FROM products WHERE name='$name' 
                      LIMIT 1") or die("Can't check name!");
      $count_name = mysql_num_rows($name_query);

  if($count_name > 0){
        $message = "This product exist!";
  }else{
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

        $query= mysql_query("INSERT INTO products (name, description, 
                price,quantity, item_number, category, image) VALUES 
                ('$name', '$descr', '$price', '$quantity', '$item_number',
                '$cat', '$image')") or die("Can't addi");
        $message="Product is added!"; 
    }
} 

?>

Theres error: Warning: file_get_contents(): Filename cannot be empty

3
  • have you put enctype="multipart/form-data" in your form?? Commented Apr 5, 2014 at 9:42
  • 1
    All mysql_* functions are deprecated and should not be used. Also addslashes is not suitable to escape data that needs to go into the database. And thirdly you should consider using the file system to save files and just store the file name in the database. Commented Apr 5, 2014 at 9:43
  • Related: stackoverflow.com/questions/3748/… Commented Apr 5, 2014 at 9:46

1 Answer 1

1

It's not recommended to add the image into the database, it's better to upload the image into the "disk" and save the reference ("/images/folder/image.jpg") into the database.

http://davidwalsh.name/basic-file-uploading-php

Sign up to request clarification or add additional context in comments.

3 Comments

for images less than 5KB can be easily manipulate with db. for more protection.
@ReNiShAR Images can just as easily be stored off the document root and have a script file access the image.
k thanks... i know.. me also used it through out my works,,,,