douzhi3454 2015-05-29 11:30
浏览 80
已采纳

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

Hello i am trying to create a subscription form, that allows a user to fill in a form and upload multiple files. already i have gotten some directions on this site as regards uploading a file and storing their paths in a database using this.

     <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.";
}
?>

this stores the path of the uploaded file in the column "photo" below

id nameMember bandMember photo aboutMember otherBands

but what i want do is have mutiple upload fields and store their paths in different columns photo, photo1 , photo2 e.g

 <input type="file" name="photo"> 
 <input type="file" name="photo1"> 
 <input type="file" name="photo2"> 
id nameMember bandMember photo photo1 photo2 aboutMember otherBands

please how do i go about this

  • 写回答

1条回答 默认 最新

  • dtjbcda841554 2015-05-29 11:35
    关注

    you have to change their 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')") ;
    

    i hope this may help another way is you can take input name as array and use loop for getting path , i recommend you to chnage the name of input type file like photo1,photo2,.....photon

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站