bobahad Posted December 18, 2007 Share Posted December 18, 2007 Hello guys, I am storing bunch of information in a mysql database. The same form used to store the information also has an image field + browse to store a picture. But of course instead of storing the pic/logo in the database, it would make better sense to store it in a folder. Now I run a loop to display all the values in the msyql table inside a well formatted table. Each row in that table starts with the logo but how do I do it so the correct logo is near the correct row. Please let me know if more clarification is needed and help is much appreciated. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 18, 2007 Share Posted December 18, 2007 You can't store an image in the database, so of course you are going to have to put it in a folder. All you have to do is store the filename of the image in the database, then you will know exactly what image goes with which row. So to display the images from the database, you would just do this. <?php echo "<img src='path_to_images/".$row['filename']."'>"; ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted December 18, 2007 Share Posted December 18, 2007 You can't store an image in the database, so of course you are going to have to put it in a folder. Yes you can store images in a database. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 18, 2007 Author Share Posted December 18, 2007 How do you store the image's filename in the database? Also the field had to be varchar ? Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 [quote]How do you store the image's filename in the database? The same as any other text. Also the field had to be varchar ? Yes. Unless you expect the path to be longer than 255 chars then you would use TEXT. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 18, 2007 Share Posted December 18, 2007 When you upload a document you can find out the image name. In the upload.php file you will be setting the destination for the uploaded file to be save in. Create a variable that puts these two bits of information together. Then insert that variable into a database. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 18, 2007 Author Share Posted December 18, 2007 Do I need a seperate form to upload a file or should the same form work, this is what I currently have. <h1>Add a Car</h1> <FORM NAME="addcarform" enctype="multipart/form-data" METHOD="Post" ACTION=""> <TABLE> <TR> <TD><B>Type:</B></TD> <TD><INPUT SIZE="6" TYPE="text" NAME="type" VALUE=""></TD> </TR> <TR> <TD><B>Make:</B></TD> <TD><INPUT TYPE="text" NAME="make" VALUE=""><br></TD> </TR> <TR> <TD><B>Model:</B></TD> <TD><INPUT TYPE="text" NAME="model" VALUE=""></TD> </TR> <TR> <TD><B>Year:</B></TD> <TD><INPUT SIZE="4" TYPE="text" NAME="year" VALUE=""></TD> </TR> <TR> <TD><B>V.I.N #:</B></TD> <TD><INPUT TYPE="text" NAME="vin" VALUE=""></TD> </TR> <TR> <TD><B>Color:</B></TD> <TD><INPUT SIZE="10" TYPE="text" NAME="color" VALUE=""></TD> </TR> <TR> <TD><B>Cost:</B></TD> <TD><INPUT SIZE="5" TYPE="text" NAME="cost" VALUE=""></TD> </TR> <TR> <TD><B>Selling Price:</B></TD> <TD><INPUT SIZE="5" TYPE="text" NAME="sellingprice" VALUE=""></TD> </TR> <TR> <TD><B>Date Bought:</B></TD> <TD><INPUT SIZE="20" TYPE="text" NAME="datebought" VALUE=""></TD> </TR> <TR> <TD><B>Link to Pictures:</B></TD> <TD><INPUT SIZE="33" TYPE="text" NAME="logolink" VALUE=""></TD> </TR> <TR> <TD><input type="hidden" name="MAX_FILE_SIZE" value="30000" /><b>Upload Logo:</b></TD> <TD><input name="userfile" type="file" /></TD> <TR/> <TR> <TD><INPUT TYPE="submit" name="add" Value="Add Car"></TD> </TR> <TR> <TD><INPUT TYPE="reset"></TD> </TR> </TABLE> <br> </FORM> Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 That form is fine. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 18, 2007 Share Posted December 18, 2007 you can either post to a php file to process the upload or just send the form to itself for processing. Go with the former idea as it is easy and less confusing. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 18, 2007 Author Share Posted December 18, 2007 Thanks guys and sorry for the trouble, one last question. So does $_FILES['textboxname'] automatically extract the file name from c:/my documents/pictures/picture1.jpg ? Cause when I hit browse, that's what it shows. Sorry I am just new to this, first semester php. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 18, 2007 Author Share Posted December 18, 2007 Why isn't this working? function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. (($type=='jpeg')?', $quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["filename"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["filename"]["tmp_name"][$key]; $name = $_FILES["filename"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); createThumbnail("/images", $name, "/images/thumbs", 120, 80); //120 = thumb width :: 80 = thumb quality (1-100) } } $query2="INSERT INTO $Table (Filename) VALUES ('$name')"; print($query2); and here's the form code <TR> <TD><input type="hidden" name="MAX_FILE_SIZE" value="30000" /><b>Upload Main Picture:</b></TD> <TD><input name="filename" type="file" /></TD> <TR/> Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 18, 2007 Author Share Posted December 18, 2007 ah sorry I forgot the include the error I am getting Warning: Invalid argument supplied for foreach() in ****/addcar.php on line 120 INSERT INTO Cars (Filename) VALUES ('') Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.