Uploading pictures into database, no error?

I’m trying to upload pictures to a database. I can display pictures but can’t upload them. Meaning I’ve put the database information right. There are no errors that I get so I’ve no idea what I’m doing wrong. Is anyone willing to help me out on this as I’m not really good with programming and my friend helped me with this.

Can anyone maybe tell me why I’m not seeing error and if possible what the error could be that would be really cool

Here is the code:

$conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit']))
{
    $tipe_file1    = $_FILES['image1']['type'];
    $tipe_file2    = $_FILES['image2']['type'];
    $tipe_file3    = $_FILES['image3']['type'];
    $tipe_file4    = $_FILES['image4']['type'];
    $prijs = $_POST['prijs'];
    $naam = $_POST['fname'];
    $beschrijving = $_POST['desc'];
    $maat1 = $_POST['maat1'];
    $maat2 = $_POST['maat2'];
    $maat3 = $_POST['maat3'];
    $maat4 = $_POST['maat4'];
    if ($tipe_file1 == "image/jpeg" || $tipe_file1 == "image/jpg"){
        $aMyUploads = array();
        foreach ($_FILES as $aFile) {
            if(0 === $aFile['error']){
                $newLocation = ''.$aFile['name'];
                if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation))){
                    $aMyUploads[] = $newLocation;
                } else {
                    $aMyUploads[] = '';
                }
            }
        }
        $stmt = $conn->prepare("INSERT INTO `producten` (naam, beschrijving, prijs, maat1, maat2, maat3, maat4, image1, image2, image3, image4) VALUES
        (:naam, :beschrijving, :prijs, :maat1, :maat2, :maat3, :maat4, :image1, :image2, :image3, :image4)");
        $stmt->execute(array(":naam"=>$naam, ":beschrijving"=>$beschrijving, ":prijs"=>$prijs, ":maat1"=>$maat1, ":maat2"=>$maat2, ":maat3"=>$maat3, ":maat4"=>$maat4, ":image1"=>$aMyUploads[0], ":image2"=>$aMyUploads[1], ":image3"=>$aMyUploads[2], ":image4"=>$aMyUploads[3]));
        }else{
        echo "<script>alert('Failed to add data! Images must be inputed at last one and filetype JPG/JPEG')</script>
            <script>setTimeout(\"self.history.back();\",0000);</script>";

Is the problem that the data does not get inserted into the database, or that the files are not moved into the correct location?

They don’t get inserted. I can see them in the database but I can’t display them, it’s like they’re bugged. I used BLOB in the database.

Hang on, if you can see them in the database, then they must have been inserted, surely?

Doesn’t the $aMyUploads[] array contain the file location, rather than the binary image data?

I thought the issue would be that the link to the image isn’t correct, taking into account that move_uploaded_file() uses absolute paths, not the relative path you need to use in the display code.

1 Like

A contradiction here :slight_smile:

I would check my source code on the page displaying them and see what it shows… I would assume blob needs converting to the image so could you post your code to read the image from the database and display it?

As @droopsnoot mentioned, you are inserting $aFile['name'] which is just the filename string, not the actual image blob.

try using varchar or text and save filename only… define absolute path for uploaded folder in a constant for displaying image later

First search for displaying BLOBs as images for php reveals this.

I changed this:

$aMyUploads = [‘test’];

And in the database I changed blob to varchar.

It displayed “Test” in the database but not the pictures

Show us the code you use to display the pictures. As it stands, you’re now storing the image filename in the database and moving the uploaded picture file into whatever directory you’ve specified in $newLocation, which in the code above seems to be nothing. So now you need to figure out exactly where those files are ending up.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.