2

I have a problem - as topic says, I need to insert images into database. Unfortunately, I don't know how.

What do I need: Browse button by which the user uploads some picture into database. If I have set my table setting correctly, data types in database should be "BLOB".

I did this and when I tried uploading in browser in my MySQL database, everything went great. Picture was uploaded and when I clicked on it, it displayed. But when I uploaded pictures by my PHP code, it's not saved in the format as it used to be, but in format ".bin".

There is my code:

this is from my class - "funkce"

mysql_query("INSERT INTO foto (id_pronajem_pokoj_nabizim, foto, foto2, foto3, foto4, foto5, foto6, foto7, foto8, foto9, foto10) VALUES ('$id_pronajem_pokoj_nabizim', '$foto', '$foto2', '$foto3', '$foto4', '$foto5', '$foto6', '$foto7', '$foto8', '$foto9', '$foto10')");

my form:

<form method="GET" action="index.php?vlozit">
<div>
            <table>
                <tr>
                    <td><h2><u>Foto:</u></h2></td>
                </tr>
                <tr>
                    <td><input type="file" name="foto"></td>
                </tr>
                <tr>
                    <td><input type="file" name="foto2"></td>
                </tr>
                <tr>
                    <td><input type="file" name="foto3"></td>
                </tr>
                <tr>
                    <td><input type="file" name="foto4"></td>
                </tr>
                <tr>
                    <td><input type="file" name="foto5"></td>
                </tr>
            </table>
        </div>
        <a href="index.php?vlozit"><input type="submit" value="Ulozit a pokracovat" name="prvni_stranka"></a>
        </form>

sending values from form:

<?php
    if (isset($_GET['prvni_stranka']))
    {
            $funkce->vloz_prvni($_GET['foto'], @$_GET['foto2'],
                        @$_GET['foto3'], @$_GET['foto4'], @$_GET['foto5'], @$_GET['foto6'], @$_GET['foto7'], @$_GET['foto8'], @$_GET['foto9'], @$_GET['foto10']);
    }
?>

I didn't know how to do this thing. That's why I have 10 rows in my table "foto" and if there are for example just 4 pictures, others are null.

Any help will be highly appreciated.

4
  • 1
    You're vulnerable to SQL injection, btw, and mysql_* functions are deprecated.
    – MightyPork
    Jul 11, 2014 at 14:33
  • To access files you musn't use $_GET but $_FILES. Don't use @. You must have enctype in your form...
    – fdehanne
    Jul 11, 2014 at 14:34
  • I really don't think you should rely on error suppression for this. I'd recommend testing the variables first, you can even do it in a loop. i.e. `for ($x=0$x<=5;$x++) { $nomvar='foto'.$x'; $$nomvar = (array_key_exists('foto'.$x, $_GET)); }; Then insert your code (note, that's an ugly solution too.. Really I'd recommend an ORM/AD system.)
    – Matt
    Jul 11, 2014 at 14:35

1 Answer 1

4

As pointed in the comment, the correct method for uploading files is POST. Still, you shouldn't be trying to insert the files into the database, but instead save the path to the uploaded images in a char/varchar field.

8
  • @user3815206 Agreed. Why would you have the need to store the entire file in the DB as opposed to the file system. Wouldn't this bog down the database? Jul 11, 2014 at 14:46
  • We're fighting this battle at work currently - with a vendor telling us they won't support their own software unless we file mammoth videos in the db and not the link. Oh, the joys of being in IT when non-IT people go out and buy software! :-( The inmates are running the asylum. Jul 11, 2014 at 14:49
  • 1
    Hate when that happens... I don't envy you, at all. Still, in this particular case you'll have to present him with evidence of the huge inconvenience this whim of his entails.
    – lucasreta
    Jul 11, 2014 at 14:54
  • Oh, so I should save just the path to pictures? I thought it won't be working so I didn't even tried it. But when I 'upload' some pictures and then I will delete them from my computer. Will it still be on the web? Jul 11, 2014 at 15:15
  • I should clarify: your db should store the path to the uploaded files, not the path to the files in the client-side.
    – lucasreta
    Jul 11, 2014 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.