0

This for array only loops once, instead of 3 (numbers of files i want to insert into the database), i've been looking at it for 2 hours now, and cant seem to locate the error. Please help.. (It's the insert for loop that only runs once, the 2 foreach works fine)

$upload = $_FILES['upload'];

$i=0;
foreach($upload['tmp_name'] as $key=>$value){
$tmp_name[$i]=$value;
$i++;
}

$i=0;
foreach($upload['name'] as $key=>$value){
$name[$i]=$value;
$i++;
}

for($i=0; $i < count($name); $i++){
    $insert_image = "INSERT INTO ".$image_table." (";

    $lastImage = end($image_rows);
    for ($i=0; $i < count($image_rows); $i++){
    $insert_image .= "".$image_rows[$i]."";
    if($image_rows[$i] != $lastImage){
        $insert_image .= ", ";
    }
    }

    $insert_image .= ") VALUE ('".$upload['name'][$i]."', '".$latest_id."')";
    mysql_query($insert_image) or die(mysql_error ());
}

HTML: <input type='file' name='upload[]'>

3
  • why three? why not for 4? i dont understand from where you are getting three. Commented Sep 29, 2013 at 14:04
  • I have 3 file uploads, therefore the $name array only has 3 file names. Commented Sep 29, 2013 at 14:07
  • Try var_dump(count($name)); after your line for($i=0; $i < count($name); $i++). What will it output? Commented Sep 29, 2013 at 14:14

1 Answer 1

5

Probably because you are reassigning the value of $i:

for ($i=0; $i < count($image_rows); $i++){
Sign up to request clarification or add additional context in comments.

2 Comments

Still only the last file gets insert in the database
Changed everything in the for loop to: $insert_image = "INSERT INTO ".$image_table." (".$image_rows.") VALUE ('".$upload['name'][$i]."', '".$latest_id."')";. Works now! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.