-2

I'm currently facing a problem. I do not know how to upload image into mysql database. And then after that i will need to retrieve from the database and display it.

It's actually because i need to create an online catalogue that includes an add to cart button. But for the images to be displayed in the online catalogue, i cannot use hard coding. I want to be able to frequently update the online catalogue whenever i have a new product in an admin page.

I can only do it for text instead of image.

if (isset($_POST['submit'])){
    $name = $_POST['name'];
    $add = mysql_query("INSERT INTO maritime_products(name) VALUES('$name')") or die("gg2");

    }

$showname = mysql_query("SELECT * FROM maritime_products");
while($anything = mysql_fetch_array($showname)){
    echo $anything['name'];
}
likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
user2594593
  • 1
  • 2
  • 2
  • Hi, there are plenty of tutorials for this out there - as it stands, this is too broad for a Stack Overflow question. Read up a bit on the subject first. Before you get started however, read [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/q/3748) – Pekka Jul 18 '13 at 08:49
  • The **Data** Base should be used for **Data**. An Image is a **File**. Therefore better use the **File** System to store such an object. – dognose Jul 18 '13 at 08:51
  • possible duplicate of [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – mike27015 Jul 18 '13 at 08:58

2 Answers2

5

You DO NOT need to store images in the database. Please, store the path to image in your database and store the image itself on the file system. Thank you!

Serge Velikan
  • 1,131
  • 15
  • 31
  • so may i ask how do i store the path to image in my database? – user2594593 Jul 18 '13 at 09:20
  • sure! First you may upload your image to your upload directory (custom) via move_uploaded_file php-func. then you can store the filename as simple text field in your database table record. When you retrieve the record's image path, you just need to concatenate your upload directory path with your image path stored in database table record. – Serge Velikan Jul 18 '13 at 09:40
  • I do not really understand what you mean. Will you mind puting the codes here? – user2594593 Jul 19 '13 at 05:47
  • you can take the base from the answer of @Asraful Haque above mine. it is very complex, but mostly right – Serge Velikan Jul 19 '13 at 07:39
0

As Serge Velikanov already said, NO GO, store your images on the server where you host your website and only save the URL of the picture into the database. Reason: If you change the size of your image, you simply upload again to the FTP and it's done, no need to save it into the database again. But if you still plan to do it, you might take a look at BLOB, OLE Object.

Pro and Cons you can find here (not written by me):

My experience has been that uploading large images or other binary objects to SQL Server is not very performant and is not completely reliable. The SQL Server team has made it a little better over the years (varbinary(max), etc.) but they haven't made a lot of improvement in performance. If you are going to do this, I would suggest chunking instead of uploading the entire image at once. Also, SQL Server 2008 has a new option (service) that you can run that allows the engine to stream Blob data to the file system. I haven't implemented this service as yet but I believe it works in conjunction with the Shadow Copy service and streams the file to the file system. I'm not clear on how backups would work but since a pointer would be stored in the table there's a good chance that the engine would know to backup the file along with the other data in the table. Check it out on MSDN or the SQL Server site. (Brian Custer)

And also again a duplicate?

https://stackoverflow.com/questions/805519/save-image-in-database

store image in database or in a system file?

Community
  • 1
  • 1
mike27015
  • 686
  • 1
  • 6
  • 19