简体   繁体   中英

multiple image upload using php mysql javascript

hi i want to upload multiple images and insert into mysql db using a single form with php, mysql and javascript. i'm applying coding. php upload will works fine. but javascript code for multiple select not working. i'm setting + and _ symbol for add or delete multiple select boxes for selection and upload in a single submit. the upload and insert db works fine.but the incement(+) symbol not working. i'm able to upload only one image at a time. i'm displayed the code below. please help me.

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();
    ?>

Change this line:

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

into:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM