0
因为我的不高兴而再次遇到困难。我不知道如果我这样做是正确的,但我需要通过foreach循环发布一个数组的id变量到MySQL数据库。如果没有任何意义,那可能是因为我缺乏用技术术语表达的能力,所以生病只是发布代码。请查看PHP代码中的注释。foreach循环中的两个数组?
任何帮助总是赞赏。
干杯,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脚本:
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