0

嗨,我想上传多个图像并使用带有 php、mysql 和 javascript 的单个表单插入 mysql db。我正在应用编码。php 上传将正常工作。但是用于多选的 javascript 代码不起作用。我正在设置 + 和 _ 符号以添加或删除多个选择框,以便在单个提交中进行选择和上传。上传和插入数据库工作正常。但是 incement(+) 符号不起作用。我一次只能上传一张图片。我显示了下面的代码。请帮我。

索引.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();
    ?>
4

1 回答 1

0

更改此行:

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

进入:

<input id="hdnLine" type="hidden" value=1>
于 2012-09-12T12:52:36.743 回答