Stevis2002 Posted November 23, 2010 Share Posted November 23, 2010 Hi all, I have the following script which uploads 1 image per testimonial, and stores the url in the database, in a field named 'Images'. I have added more upload fields to my form and named them images 2,images 3 etc, and then i tried to copy the following code to upload the urls in to the extra fields i mage for the urls in the database, ('Images2','Images3' etc etc), but i am getting a blank page now. I even copied teh function and adjusted the names...eg $imgname2, 3 etc. Can anybody help me please? Here is the code that deals with the image upload Thanks <p align="center"> </p> </body> <?php $con = mysql_connect("localhost","xxxxxxxxxxxxxxxxx","xxxxxxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxxxxxxxx", $con); $image_tmpname = $_FILES['images']['name']; $imgdir = "uploaded_images/"; $imgname = $imgdir.$image_tmpname; if(move_uploaded_file($_FILES['images']['tmp_name'], $imgname)) { list($width,$height,$type,$attr)= getimagesize($imgname); switch($type) { case 1: $ext = ".gif"; break; case 2: $ext = ".jpg"; break; case 3: $ext = ".png"; break; default: echo "Not acceptable format of image"; } $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<p align=center><b>1 testimonial added</b></p>"; mysql_close($con); ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/ Share on other sites More sharing options...
litebearer Posted November 23, 2010 Share Posted November 23, 2010 Review this (it may provide the impetus us need) http://www.plus2net.com/php_tutorial/php_multi_file_upload.php Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138405 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks lite, but i already have gone through that. I cant send the urls of each image/name into the db. That is my problem. Once i work out how to send more tahn one url into the database, then i should be ok Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138410 Share on other sites More sharing options...
litebearer Posted November 23, 2010 Share Posted November 23, 2010 You need to create a loop that will deal with each image. Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138418 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 You need to create a loop that will deal with each image. Please could you help me do that? Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138420 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 See example #3 at the following link for how you define multiple input fields and how you can loop over them in the php code - http://us.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138422 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks for that, but now i get nothing entering the database, just an error saying that the query was empty <form action="add_testimonials.php" method="post" enctype="multipart/form-data" name="add_test" id="add_test"> <p> </p> <p align="center"> <label for="customername">Customer Name:</label> <input name="customername" type="text" id="customername" maxlength="150" /> </p> <p align="center"> <label for="town">Town/City: </label> <input name="town" type="text" id="town" maxlength="150" /> </p> <p align="center"> <label for="testimonial"><u>Testimonial </u></label> </p> <p align="center"> <textarea name="testimonial" id="testimonial" cols="60" rows="10"></textarea> </p> <p align="center"> <label for="sort_order">Sort Order: </label> <input name="sort_order" type="text" id="sort_order" size="10" maxlength="3" /> </p> <p align="center"> <label for="images">Upload Image</label> <input type="file" name="images[]" /> <input type="file" name="images[]" /> <input type="file" name="images[]" /> <input type="file" name="images[]" /> <input type="file" name="images[]" /> </p> <p align="center"> <input type="submit" name="submit" id="submit" value="Submit" /> </p> <p> </p> <p> </p> </form> <p align="center"> </p> </body> <?php $con = mysql_connect("localhost","xxxxxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxxxxxxxxx", $con); foreach ($_FILES["images"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["images"]["tmp_name"][$key]; $name = $_FILES["images"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); } } //$image_tmpname = $_FILES['images']['name']; $imgdir = "uploaded_images/"; //$imgname = $imgdir.$image_tmpname; if(move_uploaded_file($_FILES['images']['tmp_name'], $imgname)) { list($width,$height,$type,$attr)= getimagesize($imgname); switch($type) { case 1: $ext = ".gif"; break; case 2: $ext = ".jpg"; break; case 3: $ext = ".png"; break; default: echo "Not acceptable format of image"; } $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<p align=center><b>1 testimonial added</b></p>"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138428 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Also, Please can somebody tell me how i can get the script to add a record if an image isn't selected? It returns that the query is empty, when really, it is only the image field which is empty. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138438 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 You would need to adapt your code that processes an uploaded image and put it inside the foreach(){... code that is executed for-each uploaded file ...} loop. And your query empty error is because you are setting the $sql variable inside of a conditional statement that is evaluating as FALSE, while your code that uses $sql is being executed unconditionally and $sql variable is empty, ergo the query is empty error. Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138453 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 I cannot get this thing right. Has anybody got an example script please? Thanks in Advance for any help Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138640 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Thankfully ,btw, this is my last question/problem....STUPID images!!!!!!...or stupid me Quote Link to comment https://forums.phpfreaks.com/topic/219574-uploading-more-than-one-image/#findComment-1138739 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.