i have a form to insert image and the title of image i make a table on the database CREATE TABLE `images` ( `image_id` int(11) NOT NULL auto_increment, `image` longlob default NULL, `title` text default NULL, ) but i donot know how to insert image into database please help
You need to insert the data from a form? INSERT INTO images (image, title) VALUES ('imagelinkhere','title') Code (markup):
this is the code of the form <body><form action="" method="post" enctype="multipart/form-data"> <table width="200" border="1"> <tr> <td>image title </td> <td><input type="text" name="textfield"></td> </tr> <tr> <td>image</td> <td><input type="file" name="file"></td> </tr> </table> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> </body>
i change the table database `image` binary but how i can retrieve it can you give me the code of retrieval????
save the image in a directory as a file and store it's name in the db each time you name the just uploaded image as max(id)+1.jpg (or gif, png etc) this is just the concept you need to send the form to a page, you can specify this in the form action, action="thefile.php", and you must use enctype="multipart/form-data" in thefile.php you must parse what you you get from the forum via the post method (you can't use GET), save and rename the file, run the insert on the db... the concept is easy, if you've never dealt with such stuff just start from somewhere and read tuts step by step for what you need...
I'd avoid actually storing the image in the DB. Just store the name of the file in the DB (or give it a new name, whatever) and put the image in a directory somewhere after upload. Then just reference the image later on using the value you stored in the DB.
I have to agree with the last two posts. Storing binary image information in a database just seems like a waste of resources to me. Why do you want the image in a data base instead of simply storing it in a directory?
When I was learning PHP, the author of the book I was reading made a comment about this. Unfortunately I can't find the exact quote right now but it was something like this... Databases are great for storing things, but if you ever find yourself wondering what's the best data type to hold an image - think again. I thought to myself, "When would you ever want to store an image in a database instead of a file?" But I guess it does happen... Gotta echo the other responses. Have your script save it in a centrally located directory (like /uploads/images) and then save the filename to the database. Much quicker/more efficient than grabbing the entire image data from the database each time. - Walkere
In order to store files like images in a database you need to use the BLOB (Binary Large OBject) datatype. I'm with the others in saying that there are better ways to upload and reference images though, as even just a handful of images has the potential to choke up your database under moderate load. Unless you have a dedicated server and prefer to do things in ways that are unecessarily complicated and inefficient, just upload the file normally and use the database to point to its location.
I'm also with everyone that has suggested not to store the image in the database. Use the table's primary key in the filename to make sure that you have a unique filename though. Saving the image in the file system will help backing up your site more manageable - storing images in the database can make the database grow very large. Brew
Below is the code I use to upload 5 image files to my server, and it automatically inserts the information to my database, I also have a script for viewing them. <?php $page_title = 'Upload a File'; include ('includes/forms.html'); $counter = 5; if (isset($_POST['submitted'])) { require_once ('includes/mysql_connect.php'); for ($i = 0; $i < $counter; $i++) { $path = '/uploads/'; $filename = 'upload' . $i; $description = 'description' . $i; $email = 'email' . $i; if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { if (!empty($_POST[$email])) { $e = "'" . escape_data($_POST[$email]) . "'"; } else { $e = 'NULL'; } if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { if (!empty($_POST[$description])) { $d = "'" . escape_data($_POST[$description]) . "'"; } else { $d = 'NULL'; } $query = "INSERT INTO uploads (email, file_path, file_name, file_size, file_type, description) VALUES ($e, $path, '{$_FILES[$filename]['name']}', '{$_FILES[$filename]['size']}', '{$_FILES[$filename]['type']}', $d)"; $result = mysql_query ($query); if ($result) { $upload_id = mysql_insert_id(); if (move_uploaded_file($_FILES[$filename]['tmp_name'], "uploads/$upload_id")) { echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>'; } else { echo '<p>File number ' . ($i + 1) . ' could not be moved.</p>'; $query = "DELETE FROM uploads WHERE upload_id = $upload_id"; $result = mysql_query ($query); } } else { echo '<p>Your submission could not be processed due to a system error. We apologise for any inconvenience.</p>'; } } } } mysql_close(); } ?> <form enctype="multipart/form-data" action="add_file.php" method="post"> <fieldset> <h3>Fill out the form to upload Your File(s):</h3> <input type="hidden" name="MAX_FILE_SIZE" value="524288" /> <?php for ($i = 0; $i < $counter; $i++) { echo '<p><b>Email Address:</b> <input type="text" name="email' . $i . '" size="40" maxlength="40" /></p> <p><b>File:</b> <input type="file" name="upload' . $i . '" /></p> <p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br /> '; } ?> </fieldset> <input type="hidden" name="submitted" value="TRUE" /> <div align="center"><input type="submit" name="submit" value="Submit" /></div> </form> <?php include ('includes/foot.html'); ?> PHP: