Page 1 of 1

Inserting image into database..

Posted: Tue Dec 13, 2005 6:04 pm
by 187skillz
Pls excuse my amateurish questions, I'm trying to make sense of this....ok

I created a table in my database

Code: Select all

create table items 
( 
itemId int auto_increment not null, 
itemName varchar(50), 
itemDesc varchar(250), 
itemPrice decimal(4,2), 
primary key(itemId), 
unique id(itemId) 
);
After that I inserted the code below.

Code: Select all

insert into items values(0, 'Tony Hawk 3', 'Tony Hawk is back. Join him in this popular skating game where speed, collisions and tricks come together to produce the best skating game of all time!', 23.95);
and another 2 code to produce this

http://www.ukbestkept.com/products.php


I need another column where I can add an image for my item.

how do I do that pls?


Also do I upload the image to my database? I've got phpmyadmin.

Thanks, I hope I made some sense and you're not lost.

Posted: Tue Dec 13, 2005 6:36 pm
by pickle
Most people are going to tell you not to upload the image right to the database. It's much faster to just upload the location of the image into the database and store the image in the filesystem.

That said, if you still want to store the image in the database, you're going to need to make a column of type blob (mediumblob ideally). Then, when you want to insert the image, read the file in as a binary string, then store that string in the database. To display the image again, read the string back, then dump it to the screen after sending header("Content-type: image/jpg"); (or whatever file type it is).

It's certainly easier to use the first method - I'd suggest going that route.

Posted: Tue Dec 13, 2005 7:01 pm
by 187skillz
pickle wrote:Most people are going to tell you not to upload the image right to the database. It's much faster to just upload the location of the image into the database and store the image in the filesystem.
.
Thanks and how do I alter my database to accomodate a table field for the image location pls?

I've got phpmyadmin, I would like to have the sql queries to do that. Thanks.

Posted: Tue Dec 13, 2005 7:52 pm
by shiznatix
just add a varchar field. i would do varchar(100) or somthing, maybe even up to 255 since I have seen people name their pictures with insaily discriptive names.

Posted: Tue Dec 13, 2005 9:54 pm
by josh
Are you going to be manually uploading the images?


Also to display the images you need to create a script, let's call it displayimg.php for example

when you output your page you select the image filename from the database and then output like this:

Code: Select all

<img src="displayimg.php<?=$filename?>" />

PHP would then read the image off the file-system, this way you aren't giving out the URL to your images. Also if you do this it's going to be easier some day to make modifications, like watermarking your images or only allowing members to see certain images for example