2012-09-12 50 views
0

您好我想上傳多個圖像,並插入到MySQL數據庫使用單一的形式與PHP,MySQL和JavaScript。我正在申請編碼。 PHP上傳將正常工作。但JavaScript代碼多選不工作。我設置了+和_符號,用於添加或刪除多個選擇框,以便在單個提交中進行選擇和上傳。上傳和插入數據庫工作正常,但incement(+)符號不起作用。我一次只能上傳一張圖片。我顯示了下面的代碼。請幫幫我。使用PHP的MySQL多圖像上傳javascript

的index.php:

<html> 
<head> 
<title>Multiple Upload</title> 
</head> 
<script language="javascript"> 
    function fncCreateElement(){ 

     var mySpan = document.getElementById('mySpan'); 

     var myLine = document.getElementById('hdnLine'); 
     myLine.value++; 

     // Create input text 
     var myElement1 = document.createElement('input'); 
     myElement1.setAttribute('type',"text"); 
     myElement1.setAttribute('name',"txtGalleryName"+myLine.value); 
     myElement1.setAttribute('id',"txt"+myLine.value); 
     mySpan.appendChild(myElement1); 

     // Create input file 
     var myElement2 = document.createElement('input'); 
     myElement2.setAttribute('type',"file"); 
     myElement2.setAttribute('name',"fileUpload"+myLine.value); 
     myElement2.setAttribute('id',"fil"+myLine.value); 
     mySpan.appendChild(myElement2); 

     // Create <br> 
     var myElement3 = document.createElement('<br>'); 
     myElement3.setAttribute('id',"br"+myLine.value); 
     mySpan.appendChild(myElement3); 
    } 

    function fncDeleteElement(){ 

     var mySpan = document.getElementById('mySpan'); 

     var myLine = document.getElementById('hdnLine'); 

     if(myLine.value > 1) 
     { 

      // Remove input text 
      var deleteFile = document.getElementById("txt"+myLine.value); 
      mySpan.removeChild(deleteFile); 

      // Remove input file 
      var deleteFile = document.getElementById("fil"+myLine.value); 
      mySpan.removeChild(deleteFile); 

      // Remove <br> 
      var deleteBr = document.getElementById("br"+myLine.value); 
      mySpan.removeChild(deleteBr); 

      myLine.value--; 
     } 
    } 
</script> 
<body> 
    <form action="upload.php" method="post" name="form1" enctype="multipart/form-data"> 
     <input type="text" name="txtGalleryName1"><input type="file" name="fileUpload1"> 
     <input name="btnCreate" type="button" value="+" onClick="JavaScript:fncCreateElement();"> 
     <input name="btnDelete" type="button" value="-" onClick="JavaScript:fncDeleteElement();"><br> 
     <span id="mySpan"></span> 
     <input name="hdnLine" type="hidden" value="1"> 
     <input name="btnSubmit" type="submit" value="Submit"> 
    </form> 
</body> 
</html> 

upload.php: 

<?php 
include("config.php"); 
     for($i=1;$i<=(int)($_POST["hdnLine"]);$i++) 
     { 
      if($_FILES["fileUpload".$i]["name"] != "") 
      { 
       if(copy($_FILES["fileUpload".$i]["tmp_name"],"shotdev/".$_FILES["fileUpload".$i]["name"])) 
       { 
        $strSQL = "INSERT INTO gallery "; 
        $strSQL .="(GalleryName,Picture) VALUES ('".$_POST["txtGalleryName".$i]."','".$_FILES["fileUpload".$i]["name"]."')"; 
        mysql_query($strSQL); 
        echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>"; 
       } 
      } 
     } 

     echo "<br><a href='view.php'>View file</a>"; 

     mysql_close(); 
    ?> 

回答

0

變化這一行:

<input name="hdnLine" type="hidden" value="1"> 

到:

<input id="hdnLine" type="hidden" value=1>