0

I have this form where i input data and then send to database but it isn't sending and also there is no error showing even after checking the Network dev tool instead it shows success which i echoed out at the end of the php file

HTML

<form id="hd_FU">
    <div class="hd_rws flx">
        <label for="code">Code</label>
        <input type="text" id="code" name="code" placeholder="...">
    </div>
    <div class="hd_rws flx mails">
        <label for="codeT">Title</label>
        <input type="text" id="codeT" name="codeT" placeholder="...">
    </div>
    <div class="hd_rws flx ai updInput">
        <small>Upload File</small>
        <input type="file" style="width: 100% !important;
        margin-left: 0;
        margin-top: 5px;" id="hd_file" accept=".pdf, .PDF, .docx, .doc" name="hd_file">
    </div>
    <button type="submit" id="hdSub" name="hdSub" class="btnSub">Submit</button>
</form>

JS

$('form#hd_FU').submit(function(e) {
    var form_data = new FormData(this);
    $.ajax({
        type: "POST",
        url: "./db_bked/uploadHD.php",
        data: form_data,
        cache:false,
        contentType: false,
        processData: false,
        beforeSend: function() {
            $("#hdSub").html('uploading<span class="pgr">....</span>');
        },
        success: function(data) {
            if(data == 'success') {
                alert("ok")
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR.responseText);
        }
    })
    e.preventDefault();
})

PHP

<?php
session_start();

require "../dbconn.inc/conn.php";

$userId = mysqli_real_escape_string($conn, $_SESSION['userId'] ?? "");

$code = $_POST['code'];
$codeT = $_POST['codeT'];

$dlr = "../PDF/handout/";
$uploadedFile = '';

$userproductImg = $_FILES["hd_file"];
$productFileName = $userproductImg['name'];
$productFileTmpName = $userproductImg['tmp_name'];
$productFileSize = $userproductImg['size'];
$productFileError = $userproductImg['error'];

$productFileExt = explode('.', $productFileName);
$productFileActualExt = strtolower(end($productFileExt));
$productFileAllowed = array('pdf', 'PDF');

if (in_array($productFileActualExt, $productFileAllowed)) {
    if ($productFileError === 0) {
        if ($productFileSize < 100000000) {
            $productFiles = uniqid(''. true).".".$productFileActualExt;
            $productFileDestination = $dlr.$productFiles;
            $uploadedFile = $productFiles;
            move_uploaded_file($productFileTmpName, $productFileDestination);
        }
    }
}


$sql = $conn->prepare("INSERT INTO hd_bk (code, userId, topic,  bk_filename) VALUES (?, ?, ?, ?)");
$sql->bind_param("ssss", $code, $userId, $codeT, $uploadedFile);
$sql->execute();

echo "success";

DATABASE

CREATE TABLE `hd_bk` (
    `bkId` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `code` text NOT NULL,
    `userId` int(11) NOT NULL,
    `topic` longtext NOT NULL,
    `bk_filename` text NOT NULL,
    `bk_date` datetime NOT NULL DEFAULT current_timestamp()
  )

Please does anyone know what i need to do cause am not seeing the data in my database - xampp - phpmyadmin

Shadow
  • 33,525
  • 10
  • 51
  • 64
brightcode
  • 39
  • 4
  • 1
    Your php code returns `'success'` at the end of the script anyway. There is absolutely no php or mysql error checking in your code, so you are completely in the dark. – Shadow Oct 26 '20 at 10:54
  • 1
    So the suggestion is to switch on PHP error reporting, and check for MySQLi errors, see the examples in the PHP manual. – KIKO Software Oct 26 '20 at 10:55
  • 1
    You should ensure PHP is set to log errors and warnings, and that mysqli is set to throw them when problems occur in the SQL. See https://stackify.com/php-error-logs-guide/ (php error logging/reporting) https://stackoverflow.com/a/14578644/5947043 (mysqli exception handling) if you need setup guides. Then run the code again and see if you have any errors logged. – ADyson Oct 26 '20 at 10:55
  • ok thanks it seems i didn't know too much about error handling i will check it and see where the error is coming from – brightcode Oct 26 '20 at 10:58
  • 1
    i used this **'mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);'** and was able to find the error thank you guys – brightcode Oct 26 '20 at 11:02

0 Answers0