各路大侠怎么实现多张图片上传到数据库中并将他们都多出来,一下是上传图片的代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta name="Keywords" content=""/>
<meta name="Description" content=""/>
<title>动态添加图片</title>
<script type="text/javascript">
function addimg(){
 //包含所有文件域的DIV
 var div = document.getElementById('imgs');
 //文件域
 var input = document.createElement("input");
 input.name = "img[]";
 input.type = 'file'; //添加
 div.appendChild(input);
 //删除按钮
 var button = document.createElement("a");
 button.href = "javascript:;";
 button.innerHTML = '删除';
 div.appendChild(button);
 //换行
 var br = document.createElement("br");
 div.appendChild(br);
 //在按钮上增加删除的事件
 button.onclick = function(){
  input.parentNode.removeChild(input);
  this.parentNode.removeChild(this);
  br.parentNode.removeChild(br);
 }}
</script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="add_product_success.php">
请选择图片:
<div id="imgs">
<input name="picture" type="file" /><br/>
</div>
<input type="button" onclick="addimg()" value="增加"/>
  <input type="submit" name="button2" id="button2" value="提交" /></form>
</body>
</html>数据库中有一个专门存放图片路径的字段是在表tbl_product中,字段名是pdtPicture
一下是提交到的页面内容,$fileinfo = $_FILES['picture'];
$path="up/".$_FILES["picture"]["name"]; //定义上传文件的路径
move_uploaded_file($fileinfo['tmp_name'],$path); //上传文件
mysql_query("insert into tbl_product (pdtSN,pdtName,pdtOrigPrice,pdtPrice,pdtIsSpecial,pdtSpPrice,pdtUnit,pdtCost,pdtIsStock,pdtStockNo,pdtPatten,pdtColor,pdtMaterial,pdtLength,pdtWidth,pdtHigh,pdtIntro,pdtIsHide,pdtSuSN,pdtSeSN,pdtStSN,pdtClickNo,pdtSaleNo,pdtIsHot,pdtIsRecommend,pdtMemo,pdtOrder,pdtPicture) values('$pdtSN','$pdtName','$pdtOrigPrice','$pdtPrice','$pdtIsSpecial','$pdtSpPrice','$pdtUnit','$pdtCost','$pdtIsStock','$pdtStockNo','$pdtPatten','$pdtColor','$pdtMaterial','$pdtLength','$pdtWidth','$pdtHigh','$pdtIntro','$pdtIsHide','$pdtSuSN','$pdtSeSN','$pdtStSN','$pdtClickNo','$pdtSaleNo','$pdtIsHot','$pdtIsRecommend','$pdtMemo','$pdtOrder','$path')");按照这样的方法做完之后提示信息是成功的,但是我到后台数据库中看到时候在保存图片路径的字段中只有一张图片上传成功了,这是什么原因造成的呢,是不是那个存储路径的字段只能存储一张图片,要上传多张图片的话就要多个保存图片路径的字段...............望各路达人排忧解难,滴水之恩当涌泉相抱

解决方案 »

  1.   

    input.name = "img[]";
    <input name="picture" type="file" />
    $fileinfo = $_FILES['picture'];
    对比一下你这三处写的,不一致
      

  2.   

    我现在改过了,input.name = "picture";
    <input name="picture" type="file" />
    $fileinfo = $_FILES['picture'];
    可结果在数据库中看的时候还是只有一张照片的路径子在里面其他的都找不到
      

  3.   

    我现在改过了,input.name = "picture";
    <input name="picture" type="file" />
    $fileinfo = $_FILES['picture'];
    可结果在数据库中看的时候还是只有一张照片的路径子在里面其他的都找不到
      

  4.   

    要上传多个必须这样
    input.name = "picture[]";
    <input name="picture[]" type="file" />上传部分应该:$fileinfo = $_FILES['picture'];
    foreach($fileinfo['name'] as $k => $fname){
      $path="up/".$fname; //定义上传文件的路径 
      move_uploaded_file($fileinfo['tmp_name'][$k],$path); //上传文件
      mysql_query(....); 
    }
      

  5.   

    你好,我按照你说的那样做了,可是在数据库中存放图片路径的那个字段里面却只有一张图片的路径,就是这样的 “up/1111111111.png”这个是那个存放图片路径的字段里面的值 
      

  6.   

    1、input.name = "img[]";
    <input name="picture" type="file" />
    $fileinfo = $_FILES['picture'];  这三处不一至,同意ls+N
    但还不能解决根本问题。2、你想用mutipart传file文件,一个文件就传一个数组过去,比如打印$fileinfo。可以看出里面有几项数据,name,size……。
    但如果传过去多个数据file,其实它算是被看成一个对象了,mutipart不知道能解析过来,反正我解析不过来。你可以试试。3、几个对象倒是传过来了。所以用处理数据时循环可以的。代码如下,作一点点小小的改动。
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <meta name="Keywords" content=""/>
    <meta name="Description" content=""/>
    <title>动态添加图片</title>
    <script type="text/javascript">
    var a=2;
    function addimg(){
     //包含所有文件域的DIV
     var div = document.getElementById('imgs');
     //文件域
     var input = document.createElement("input");
     input.name = "picture"+a;
     input.type = 'file';
     a++;
     //添加
     div.appendChild(input);
     //删除按钮
     var button = document.createElement("a");
     button.href = "javascript:;";
     button.innerHTML = '删除';
     div.appendChild(button);
     //换行
     var br = document.createElement("br");
     div.appendChild(br);
     //在按钮上增加删除的事件
     button.onclick = function(){
      input.parentNode.removeChild(input);
      this.parentNode.removeChild(this);
      br.parentNode.removeChild(br);
     }}
    </script>
    </head>
    <body>
    <form method="POST" enctype="multipart/form-data" action="upload_file.php">
    请选择图片:
    <div id="imgs">
    <input name="picture1" type="file" /><br/>
    </div>
    <input type="button" onclick="addimg()" value="增加"/>
      <input type="submit" name="button2" id="button2" value="提交" /></form>
    </body>
    </html>
    for($i = 1 ;$i < 10 ; $i++ ){
    //10自己设定,一次性提交图片限定值。
      $fileinfo = $_FILES["picture$i"];
      $path="upload/".$_FILES["picture$i"]["name"];    //定义上传文件的路径        
      move_uploaded_file($fileinfo['tmp_name'],$path);    //上传文件
      
      //数据库执行语句(略)
    }
      

  7.   

    那个去掉,sorry,
      

  8.   


    如果设定了10个而只上传了5个,那岂不是要插入5条空数据?
    mockey的正解。。
      

  9.   


    $_FILES数组里有size和error
    你可以判断一下size和error,对于上传大小为0的、有错误的则认为是上传失败的,不添加到数据库里即可