1

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>
gammer
  • 15
  • 5
  • Where do you output that error? Do you clear `$_SESSION['error']` before you set it? – brombeer Jul 27 '21 at 05:46
  • It does not affect the function, even though i deleted that part of code, the error message still show and no data has been inserted to the database – gammer Jul 27 '21 at 05:52
  • Are you using conventional FORM to submit or are you using ajax FormData +append to do the file upload ? (there is no such information in your shared codes) – Ken Lee Jul 27 '21 at 06:57
  • I am using this ```
    ``` after i impliment this there's no error message anymore but still no data has been added to the database
    – gammer Jul 27 '21 at 07:12
  • what is the field type of `file` in your shortTest table ? are you sure it is sufficient to store the pdf file data ? – Ken Lee Jul 27 '21 at 07:19
  • I am using LongBlob – gammer Jul 27 '21 at 07:23
  • OK. Please add a short statement `echo "test"; ` into the if($certcheck){ block , and try upload a pdf , is it true that the screen shows "test" ? – Ken Lee Jul 27 '21 at 07:27
  • Yes, the test can be echo – gammer Jul 27 '21 at 07:47
  • the output are very weird I have no idea what it is ```INSERT INTO shorttest (file,dtype) VALUES ('%PDF-1.4 1 0 obj << /Title (��) /Creator (��wkhtmltopdf 0.12.3) /Producer (��Qt 4.8.7) /CreationDate (D:20210706145936+02'00') >> endobj 3 0 obj << /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 10 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 11 0 R /Resources 13 0 R /Annots 14 0 R /MediaBox [0 0 595 842] >> endobj 13 0 obj << /ColorSpace << /PCSp 4 0 R /CSp ``` – gammer Jul 27 '21 at 07:56
  • The output has too many character and cannot be included here – gammer Jul 27 '21 at 07:56
  • It appears to be fine. For the $dbh, are you sure the username and password are all correct ? – Ken Lee Jul 27 '21 at 07:59
  • Yes, because I'm using my previous config file which is in the same database – gammer Jul 27 '21 at 08:03
  • Hi I have modified my code to this ```$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 !";``` and it works BUT only the first file are able to be inserted. – gammer Jul 27 '21 at 08:14
  • yea but the problem has not been solved yet...... I wanted to add multiple file using dynamic field. Only the first data are able to be inserted – gammer Jul 27 '21 at 08:20
  • u mean close } after ```$query->execute();``` ? – gammer Jul 27 '21 at 08:37
  • Closed already, but it is still the same – gammer Jul 27 '21 at 08:47
  • Try this [link](https://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query) – Ken Lee Jul 27 '21 at 08:54

0 Answers0