есть трудности из-за моего noobness, снова. Я не знаю, выполняю ли я это правильно, но мне нужно отправить переменную id с массивом через цикл foreach в mysql db. Если это не имеет никакого смысла, то, вероятно, из-за моей неспособности сформулировать с техническим жаргоном, так что просто отправьте код. Просмотрите примечания в коде PHP.Два массива в петле foreach?
Любая помощь всегда ценится.
Приветствия, Lea
ФОРМА:
<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
}
Отлично! Спасибо! И спасибо за объяснение, где я ошибся :) – Lea 2010-11-27 00:53:52