Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3480640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:22:22+00:00 2026-05-18T10:22:22+00:00

having difficulties because of my noobness, again. I dont know if I am doing

  • 0

having difficulties because of my noobness, again. I dont know if I am doing this right, but I need to post an id variable with an array through a foreach loop to a mysql db. If that made no sense, its probably due to my lack of ability to articulate with technical jargon, so ill just post the code. Please view the notes in the PHP code.

Any help always appreciated.

Cheers, Lea

FORM:

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">

 <?php
    $num = 0;
    while($num < $num_uploads)
    {
        ?>
<input type="hidden" name="item_id[]" value="<?php echo $stockno; ?>" />
<input type="file" id="myfile" name="userfile[]" size="40">

        <?php $num++;
    }
 ?>
<input type="submit" name="Preview" value="Preview" />
</form>

PHP SCRIPT:

if(isset($_POST['Preview']) ) {
// START PHOTO QUERY

    if(isset($_FILES['userfile']['tmp_name']))
    {
        /** loop through the array of files ***/
        for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
        {
            // check if there is a file in the array
            if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            /*** check if the file is less then the max php.ini size ***/
            elseif($_FILES['userfile']['size'][$i] > $upload_max)
            {
                $messages[] = "File size exceeds $upload_max php.ini limit";
            }
            // check the file is less than the maximum file size
            elseif($_FILES['userfile']['size'][$i] > $max_file_size)
            {
                $messages[] = "File size exceeds $max_file_size limit";
            }
            else
            {
                // copy the file to the specified dir 
                if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['userfile']['name'][$i].' uploaded';
                    $name[] = $_FILES['userfile']['name'][$i];
                    $id[] = $_POST['item_id'];

// HAVING DIFFICULTIES HERE

foreach( $name as $value ) {
$sql = "INSERT INTO stock_photos (photo_filename) VALUES ('$value')";
mysql_query($sql);
foreach( $id as $val ) {
$sql2 = "UPDATE stock_photos SET photo_item_id = '$val' WHERE photo_filename = '$value'";
mysql_query($sql2) or die(mysql_error());
}
}
// END DIFFICULTIES HERE
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
                }
            }
        }
    }

// END PHOTO QUERY
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:22:22+00:00Added an answer on May 18, 2026 at 10:22 am

    I think you have a flaw in your logic. You don’t want to have a nested foreach loop. Think about it:

    You are looping over each filename. For each filename, you are looping over all IDs and update the filename ID with with each value from the ID array. You are basically overwriting the ID in the database with every UPDATE call, eventually setting the value to the last value of the $id array.

    Thus, you actually should have the loops one after each other.


    Assuming your $name and $id are filled properly and both contain the same number of items, you can do even better, using a normal for loop:

    for($i = 0; $i < count($name); $i++ ) {
        $sql = "INSERT INTO stock_photos (photo_filename, photo_item_id) VALUES ('".   mysql_real_escape_string($name[$i]) . "', '" . mysql_real_escape_string($id[$i] . "')";
        mysql_query($sql);
    }
    

    Note: Never never never trust user input, so take precautions through filtering and escaping like I did with mysql_real_escape_string().


    Update:

    And you can do even better with just one MySQL query by inserting multiple values at once:

    function prepare($name, $id) {
        return sprintf("'%s', '%s'",
               mysql_real_escape_string($name),
               mysql_real_escape_string($id));
    }
    
    $values = array_map('prepare', $name, $id); 
    $sql = 'INSERT INTO stock_photos (photo_filename, photo_item_id) VALUES (' . implode('),(', $values) . ')';  
    mysql_query($sql);
    

    Reference: sprintf, array_map, implode

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have having difficulties on this one page login.php. Because of the nature of
I am having difficulties in disabling href links through jquery. I am using this
I am having difficulties achieving Css 100% so my website can look right in
This is probably something quite easy to resolve, however, I am having some difficulties
I am having difficulties with listing this type of data. Scenario is as follows:
I am having difficulties with this code where I'm trying to put a certain
im having difficulties figuring this out, ive looked at examples here and on the
I'm attempting to solve the below exam question but I'm having difficulties with it.
I am having difficulties figuring this out. I can see I am posting XML
I'm having difficulties with using internal imports inside my projects. This is a partial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.