如何上传多个文件,将它们的路径存储在 mysql 数据库行的不同列中
how to upload multiple files, store their paths in different columns in a mysql database row
您好,我正在尝试创建一个订阅表单,允许用户填写表单并上传多个文件。
我已经在这个网站上得到了一些关于使用它上传文件和将它们的路径存储在数据库中的指导。
<form method="post" action="addMember.php" enctype="multipart/form-data">
<p>
Please Enter the Band Members Name.
</p>
<p>
Band Member or Affiliates Name:
</p>
<input type="text" name="nameMember"/>
<p>
Please Enter the Band Members Position. Example:Drums.
</p>
<p>
Band Position:
</p>
<input type="text" name="bandMember"/>
<p>
Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<p>
Please Enter any other information about the band member here.
</p>
<p>
Other Member Information:
</p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
<p>
Please Enter any other Bands the Member has been in.
</p>
<p>
Other Bands:
</p>
<input type="text" name="otherBands" size=30 />
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
</form>
and the php code for inserting this
<?php
//This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];
// Connects to your Database
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
下面"photo"列中存储了上传文件的路径
id nameMember bandMember photo aboutMember otherBands
但我想要做的是拥有多个上传字段并将它们的路径存储在不同的列 photo, photo1 , photo2
例如
<input type="file" name="photo">
<input type="file" name="photo1">
<input type="file" name="photo2">
id nameMember bandMember photo photo1 photo2 aboutMember otherBands
请问我该怎么做
你必须改变他们的 as
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
for($i=1;$i<=3;$i++)
{
if(move_uploaded_file($_FILES['photo'.$i]['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
and in your query you can change it with like a
mysql_query("INSERT INTO tableName
(nameMember,bandMember,photo,photo1,photo2,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic','$pic1','$pic2', '$about', '$bands')") ;
我希望这可能会有所帮助,另一种方法是您可以将输入名称作为数组并使用循环获取路径,我建议您更改输入类型文件的名称,例如 photo1、photo2、.....photon
您好,我正在尝试创建一个订阅表单,允许用户填写表单并上传多个文件。 我已经在这个网站上得到了一些关于使用它上传文件和将它们的路径存储在数据库中的指导。
<form method="post" action="addMember.php" enctype="multipart/form-data">
<p>
Please Enter the Band Members Name.
</p>
<p>
Band Member or Affiliates Name:
</p>
<input type="text" name="nameMember"/>
<p>
Please Enter the Band Members Position. Example:Drums.
</p>
<p>
Band Position:
</p>
<input type="text" name="bandMember"/>
<p>
Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<p>
Please Enter any other information about the band member here.
</p>
<p>
Other Member Information:
</p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
<p>
Please Enter any other Bands the Member has been in.
</p>
<p>
Other Bands:
</p>
<input type="text" name="otherBands" size=30 />
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
</form>
and the php code for inserting this
<?php
//This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];
// Connects to your Database
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
下面"photo"列中存储了上传文件的路径
id nameMember bandMember photo aboutMember otherBands
但我想要做的是拥有多个上传字段并将它们的路径存储在不同的列 photo, photo1 , photo2 例如
<input type="file" name="photo">
<input type="file" name="photo1">
<input type="file" name="photo2">
id nameMember bandMember photo photo1 photo2 aboutMember otherBands
请问我该怎么做
你必须改变他们的 as
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
for($i=1;$i<=3;$i++)
{
if(move_uploaded_file($_FILES['photo'.$i]['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
and in your query you can change it with like a
mysql_query("INSERT INTO tableName
(nameMember,bandMember,photo,photo1,photo2,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic','$pic1','$pic2', '$about', '$bands')") ;
我希望这可能会有所帮助,另一种方法是您可以将输入名称作为数组并使用循环获取路径,我建议您更改输入类型文件的名称,例如 photo1、photo2、.....photon