-1

i am able to store all fields except $binarydata it keeps saving as an empty cell in the database. i had working code before and saw the track image column light up with a BLOB data BUT NOW for some reason it wont insert the BLOB. any help would be greatly appreciated

   if (isset($_POST['submit'])) {
        // Taking all 4 values from the form data(input)
        $trackname = $_POST['trackname'];
        $descr = $_POST['descr'];
        $tags = $_POST['tags'];
        $genre = $_POST['genre'];
        $artist = $username;

        // Check if a file was uploaded successfully
        if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
            // Get the temporary file name
            $file_tmp = $_FILES['file']['tmp_name'];

            // Open the file in binary mode and read the data
            $binaryData = file_get_contents($file_tmp);
        } else {
            // If no file was uploaded or an error occurred, set $binaryData to NULL
            $binaryData = NULL;
        }

        // Performing insert query execution
        // Here, we explicitly specify the columns we are inserting into (excluding user_id)
        $sql = "INSERT INTO tracks (trackname, descr, tags, genre, artist,trackimage) VALUES (?, ?, ?, ?, ?, ?)";

        // Prepare the SQL statement
        $stmt = mysqli_prepare($conn, $sql);
        

        if ($stmt === false) {
            // Handle the error if the statement preparation fails
            die("ERROR: Could not prepare the SQL statement. " . mysqli_error($conn));
        }

        // Bind the parameters to the statement, "b" is used for BLOB data
        mysqli_stmt_bind_param($stmt, "sssssb", $trackname, $descr, $tags, $genre, $artist, $binaryData); 
        $stmt->send_long_data(1, $binaryData);

        // Execute the statement
        if (mysqli_stmt_execute($stmt)) {
            $stmt->execute(


FIXED:


// Check if a file was uploaded successfully
        if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
            // Get the temporary file name
            $file_tmp = $_FILES['file']['tmp_name'];

            // Open the file in binary mode and read the data
            $binaryData = fopen($file_tmp, 'rb');

            // Get the contents of the file as a string
            $file_contents = fread($binaryData, filesize($file_tmp));

        mysqli_stmt_send_long_data($stmt, 5, $file_contents);



Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Unless there is some kind of blatant syntax error in your code, it is difficult for us to tell you what went wrong. Did you check if reading the file contents were successful? – Shadow Aug 07 '23 at 06:56
  • It can always help to [check for errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). Even to know there are no errors is informative. – KIKO Software Aug 07 '23 at 06:58
  • 3
    `// Open the file in binary mode and read the data` - you are only doing the first part of what that comment says there - you are opening the file. But where is the part where you actually read its content ...? – CBroe Aug 07 '23 at 07:36
  • thanks for the suggestions. ive update the binary data to file_get_contents and add get_long_data to hopefully help give it the push to the DB still no success. any help with the edited code would help @CBroe – Dane De Pasquale Aug 07 '23 at 16:48
  • Have you verified the file upload worked correctly to begin with? Do a `var_dump($_FILES['file']);` – CBroe Aug 08 '23 at 05:43

0 Answers0