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

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算