我有一个包含各种字段的表单,用户可以通过选择附加图像数量来感受:
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
提交后,它会在数据库中创建一个名为“album”的表,如下所示:
function create_album($params)
{
db_connect();
$query = sprintf("INSERT INTO albums set
albums.title = '%s',
albums.email = '%s',
albums.discuss_url = '%s',
albums.theme_id = '%s',
albums.fullname = '%s',
albums.description = '%s',
created_at = NOW()",
mysql_real_escape_string($params['title']),
mysql_real_escape_string($params['email']),
mysql_real_escape_string($params['discuss_url']),
mysql_real_escape_string($params['theme_id']),
mysql_real_escape_string($params['fullname']),
mysql_real_escape_string($params['description'])
);
$result = mysql_query($query);
if(!$result)
{
return false;
}
$album_id = mysql_insert_id();
return $album_id;
}
我想要文件,但要转到“图片”表并链接到正确的相册。
$create_album = create_album($_POST['album']);
mysql_query( "INSERT INTO images(`name`,`album_id`) VALUES('$newName', '$create_album')" );
我遇到的问题是将这些多个图像(用户可以选择提交一个或两个或三个文件)附加一个表单提交到一个相册。现在,如果用户提交3个文件,则会在每个表单提交时创建3个相册。
最后,这是我的数据库结构:
CREATE TABLE `albums` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(50) NOT NULL,
`fullname` varchar(40) NOT NULL,
`discuss_url` varchar(150) NOT NULL,
`email` varchar(100) NOT NULL,
`created_at` datetime NOT NULL,
`theme_id` int(11) NOT NULL,
`description` int(11) NOT NULL,
`vote_cache` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
CREATE TABLE `images` (
`id` int(11) NOT NULL auto_increment,
`album_id` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
答案 0 :(得分:1)
我只想对此问题进行快速更新。也许我没有提供足够的信息,这对于查看问题非常重要。本文非常解释well。基本上,每次提交文件时都会创建相册,而不是表单。
所以这是我的表格(工作):
<?php if (!isset($_POST['btnSubmit'])) { ?>
<form action="index.php?view=create" method="post" enctype="multipart/form-data">
<fieldset>
<div>
<label><b>Title:</b></label>
<input name="album[title]" size="40" type="text" value="" class="textfield" />
</div>
<div>
<label><b>Fullname</b></label>
<input name="album[fullname]" size="40" type="text" value="" class="textfield" />
</div>
<div>
<label><b>Attach Photo</b> </label>
<input type="hidden" name="" value="" />
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
这是php(我删掉了一些,因此更容易):
//If form was submitted
if (isset($_POST['btnSubmit'])) {
create_album($_POST['album']);
$find_album_id = mysql_insert_id();
/*** check if a file has been submitted ***/
if(isset($_FILES['userfile']['tmp_name']))
{
/** loop through the array of files ***/
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
$ext = strrchr($imgName, ".");
// then create a new random name
$newName = md5(rand() * time()) . $ext;
if (is_image_types($_FILES['userfile']['type'][$i])
and is_valid_file_size($_FILES['userfile']['size'][$i])
and is_uploaded_file($_FILES['userfile']['tmp_name'][$i])
and is_valid_width_height($_FILES['userfile']['tmp_name'][$i])
)
{
mysql_query("INSERT INTO images(name, album_id) VALUES('$newName', '$find_album_id')") ;
copy($_FILES['userfile']['tmp_name'][$i], './photos/'.$original_dir.'/' .$newName.'.jpg');
}
else
{
$warning = "Click back error uploading file.
Please make sure your file is a JPEG and less than 1MB";
}
}
}
}
break;
}
我希望它可以帮助任何正在寻找的人......