Mr Chris Posted April 12, 2009 Share Posted April 12, 2009 Hello, I have a form where I upload each image on a page, and using a foreach loop upload the image name of the file to my database. Now, i've modified the code below and with each image I also want to upload a picture reference (ref) to my database for each pic uploaded. How can I also get this in my foreach loop? Many Thanks <?php if (isset($_POST['Submit'])) { // set the image details and upload the file foreach ($_FILES['ufile']['name'] as $key => $val) { move_uploaded_file ($_FILES['ufile']['tmp_name'][$key], $picture); // and insert into the database $result = mysql_query("Insert into images (picture, ref) values('". basename($picture) ."', '$ref')") or die(mysql_error()); } } ?> <form action="" method="post" enctype="multipart/form-data" name="myform" id="myform"> <?php $number = $_GET['num']; for($i = 0; $i <= $number; $i++) { echo " <p>Upload Image (".$i."):<input name=\"ufile[]\" style=\"width:300px;\" type=\"file\" id=\"ufile[]\" /></p> <p>Ref: <input name=\"ref[]\" class=\"big\" type=\"text\" id=\"ref[]\" style=\"width: 300px\"/></p>\n"; } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/ Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 $result = mysql_query("Insert into images (picture, ref) values('". basename($picture) ."', '$ref')") or die(mysql_error()); Is that not what you're doing there? Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807797 Share on other sites More sharing options...
ToonMariner Posted April 12, 2009 Share Posted April 12, 2009 when you are looking to do multiple inserts into a database its best to do it in one query rather than running lots of queries in a loop - not very efficient. this code here <?php .... foreach ($_FILES['ufile']['name'] as $key => $val) { move_uploaded_file ($_FILES['ufile']['tmp_name'][$key], $picture); // and insert into the database $result = mysql_query("Insert into images (picture, ref) values('". basename($picture) ."', '$ref')") or die(mysql_error()); } .... ?> 1st off I don't see $picture changing on each loop which would mean you overwrite your image all the time.. ok the query.. <?php .... $qry = NULL; foreach ($_FILES['ufile']['name'] as $key => $val) { move_uploaded_file ($_FILES['ufile']['tmp_name'][$key], $picture); // and insert into the database $qry .= "Insert into images (picture, ref) values('". basename($picture) ."', '$ref'),"; } $qry = substr($qry,0,strlen($qry) -1); // remove last comma. $result = mysql_query($qry) or die(mysql_error()); .... ?> now like I said - each time you run through this loop you are NOT updating $picture so your database records wont be what you expect and each file you upload will overwrite the previous file.. revisit that part and all should be well. the qry I did above is just to help you improve performance and save your server some hassel. Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807803 Share on other sites More sharing options...
Mr Chris Posted April 12, 2009 Author Share Posted April 12, 2009 That's but the code above is a snippett, not the full code. Should have been more specific on that, sorry. Each pic is uploaded and renamed without being overwritten. The code above uploads each individual image for the picture, but not for each reference associated with the picture as well. I know in this line of the foreach: foreach ($_FILES['ufile']['name'] as $key => $val) { Its saying for each uploaded picture, but I need it to say for each uploaded picture and gallery reference? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807807 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 Uhh...I don't think you can have a foreach() statement depending on two arrays. Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807810 Share on other sites More sharing options...
ToonMariner Posted April 12, 2009 Share Posted April 12, 2009 if your refences are in an array indexed with matching indices to the ufile array then its easy.. foreach ($_FILES['ufile']['name'] as $key => $val) { $ref = $_POST['ref'][$key]; ... Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807812 Share on other sites More sharing options...
Mr Chris Posted April 12, 2009 Author Share Posted April 12, 2009 Thanks ToonMariner, thats exactly what I wanted - seems simple now! Thanks all as well Quote Link to comment https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807826 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.