Foreach döngüsünde iki diziler?

0 Cevap php

Yine, çünkü benim noobness zorlukları olan. Ben bu hakkı yapıyorum, bilmiyorum, ama ben bir mysql db için bir foreach döngü bir dizi ile id değişkeni göndermek gerekir. Hiçbir mantıklı olursa, onun muhtemelen teknik jargon ile ifade yeteneği benim eksikliği, çok kötü sadece kod sonrası. PHP kodu notlarını görüntülemek edin.

Herhangi bir yardım her zaman takdir.

Alkış, 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
}

0 Cevap