如何上传多个文件,将它们的路径存储在mysql数据库行的不同列中



你好,我正在尝试创建一个订阅表单,允许用户填写表单并上传多个文件。我已经在这个网站上得到了一些关于上传文件和使用这个在数据库中存储路径的指导。

     <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

请告诉我该怎么做

你必须把它们改成

$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

相关内容

  • 没有找到相关文章

最新更新