I'm trying to insert multiple file into the database by using the dynamic input field created. Is there any way that can insert the file type into the database ? I have tried by using the code below but only the first file can be inserted.
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 !";
}
}
}
Style
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>