首页 > 解决方案 > 使用动态输入字段将文件和文件类型插入mysql数据库

问题描述

我正在尝试使用创建的动态输入字段将多个文件插入数据库。有什么方法可以将文件类型插入数据库?我尝试过使用下面的代码,但只能插入第一个文件。

PHP

if($q >= 0){
        for($i=0; $i<=$q; $i++){
            $certcheck = true;
            $upload = end(explode(".", $_FILES['cert']['name'][$i]));
            $imageType = $_FILES['cert']['type'][$i];
            $fileType = array("png", "jpg", "jpeg", "pdf");
            if (in_array(strtolower($upload), $fileType)) {
                $cert1 = file_get_contents($_FILES['cert']['tmp_name'][$i]);
            }
            else {
                $error = " The file is not an image or pdf. Please upload again";
                $certcheck = false;
                $_SESSION['error'] = $error;
            }    

            $cert = $cert1;
            $dtype = $imageType;
            $sql3="INSERT INTO shorttest (cert,dtype) VALUES (:cert,:dtype)";
            $query = $dbh->prepare($sql3);
            $query->bindParam(':cert', $cert, PDO::PARAM_STR);
            $query->bindParam(':dtype', $dtype, PDO::PARAM_STR);
            $query->execute();
            $msg = "Your Application Has Been Sent !";
        }
    

    }    
                                                    
    }         

风格

var q = 0;

        function qualification() {
            q++;
            var copyContent= "<div class='row' name='rows' style='margin-top:-20px;'>";
            copyContent= "<tr><td><input class='tableBody' type='file' id='cert"+q+"' name='cert[]'></td>";
            copyContent += "<td><a href='javascript:void(0);' class='remove' style='cursor:pointer'><i class='material-icons' title='Delete item'>remove_circle_outline</i></a></td></tr></div>";
            $('#tbl_qualification').append(copyContent);

            document.getElementById("q").value = q;
        }

HTML

  <table id="tbl_qualification"style="margin-top:25px;margin-bottom:25px;">
                                                <tr class="tableTitle">
                                                    <th style="width:200px"><center>Document</th>
                                                    <th style="width:20px;"></th>
                                                </tr>
                                                <tr>
                                                    <td><input class="tableBody" type="file" id="cert0" name="cert[]"></td>
                                                </tr>
                                                
                                                </table>
                                                <div style="margin-right:-1200px">
                                                    <a onclick="qualification()" style="cursor:pointer"><i class="material-icons" title="Add item">add_circle_outline</i></a> 
                                                </div> 
                                                <input name="q" type="text" id="q" readonly hidden>

标签: phpmysql

解决方案


推荐阅读