Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/153716-solved-for-each-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807803
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/153716-solved-for-each-help/#findComment-807807
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.