1

I have some problems here and I hope someone can help me. unfortunately I couldn't find a solution yet so you are my last hope.

I upload an image to my database but I cant display it. What I get is this icon when an image cant be find or load, but my database is full of pictures :-/

This is my PHP code:

<?php

$dbhost = "localhost"; 
$dbuser = "DBConnector"; 
$dbpassword = "root"; 
$db = "phplogin";
$b = $_POST['bildbestaetigen'];

if(isset($b)){

if(getimagesize($_FILES['bild']['tmp_name'])== FALSE)
{
    echo "Choose a picture.";
}
else
{
    $image= addslashes($_FILES['bild']['tmp_name']);
    $name = addslashes($_FILES['bild']['name']);
    $image= file_get_contents($image);
    $image= base64_encode($image);
    saveimage($name,$image);
}

}

displayimage();


    function saveimage ($name,$image){

    $link = mysqli_connect("localhost","root","") or die("Verbindung zur Datenbank konnte nicht hergestellt werden!");
    mysqli_select_db($link,"phplogin") or die ("kann nich finden ");
    $result= mysqli_query($link, "insert into bilder (name,bild) values ('".$name."','".$image."')");

    if($result)
    {

        echo "Image uploaded";

    }
    else
    {
        echo "<br/> Image not uploaded";
    }

    }


function displayimage(){
$link = mysqli_connect("localhost","root","") or die("Verbindung zur    Datenbank konnte nicht hergestellt werdengjghghghh");
    mysqli_select_db($link,"phplogin") or die ("kann nich finden ");

    $result = mysqli_query($link,"SELECT * from bilder");
    while( $row = mysqli_fetch_array($result));
    {
    echo '<img height="300"  width="300" src="data:bild;base64,'.$row[2].'         ">';

    }
    mysqli_close($link);    
}


?>

I tried so many things with out luck.

I hope someone can help me!

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
DaviDave
  • 13
  • 1
  • 5
  • 1
    I wouldn't store the image itself in the database but rather a reference to where the image can be found on disk. e.g just store '/path/to/image.jpg' in the image column. – Tim Ackroyd Jul 06 '15 at 22:56
  • I think this other SO post will be exactly what you need - in short, you need to rearrange your code slightly, but you can get it donehttp://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag – David W Jul 06 '15 at 23:02
  • @TimAckroyd can you show me hot to do it ? – DaviDave Jul 06 '15 at 23:28
  • @DavidW hey i tried it but the result is the same :( – DaviDave Jul 06 '15 at 23:29
  • Well, the issue I referenced was marked a solution for that problem, so without seeing your implementation it would be hard to say why yours failed. Are you sure the image data in your database is correct? – David W Jul 06 '15 at 23:38
  • yes i think so ..i have got 3 columns : id, name and blob. i tried to rebuild this tutorial https://www.youtube.com/watch?v=4ZpqQ3j1o2w i had to change a few things but it should work =( – DaviDave Jul 06 '15 at 23:47
  • Here is another solution tho go with mysqli. http://php.about.com/od/phpwithmysql/ss/Upload_file_sql.htm – Drew Jul 06 '15 at 23:57
  • It would seem it would take 10, 15 minutes max. Picture saved not in blob – Drew Jul 06 '15 at 23:58
  • @DrewPierce it tried it but it dont work =( =( okay..i get this message "The file aaa.JPG has been uploaded, and your information has been added to the directory" . yes ..it has been added to the directory but no it has not been uploaded :D damn -.- – DaviDave Jul 07 '15 at 00:53

2 Answers2

0

Use this W3Schools tutorial on PHP uploads. As mentioned in comments, save the image path in database, not file itself which may require the blob data type and can bloat your database very quickly.

To do so, upload the user-selected file from your HTML form onto your webserver as a physical file by specifying a target folder with $_FILES array (be sure folder exists) and using the move_uploaded_file() function:

$targetfile = "uploads/" .basename($_FILES['bild'])
...
# FILE VALIDATION
...
move_uploaded_file($_FILES['bild']['tmp_name'], $targetfile)

Then, save this image path (text string) into your database (assuming here bild is a text data type):

insert into bilder (name,bild) values ('".$name."','".$targetfile."')

Finally, just as you did before fetch records from the database table and echo the image path in src attribute of html's img tag (by the way filter the SQL query to particular user or image id as multiple records may output):

echo '<img height="300" width="300" src="'.$row[2].'">';

And remember the $targetfile variable will contain full, absolute path of image location.

Parfait
  • 104,375
  • 17
  • 94
  • 125
  • okay thank you so much for your answer i will try it tomorrow =) my goal is to create an image gallery where the pics are next to each other. like 5 pics in a row andthen the next row. would it be possible like this ? – DaviDave Jul 07 '15 at 01:20
  • hey please take a look below. i have posted my new code – DaviDave Jul 07 '15 at 12:24
0

Hi, assuming you have correctly uploaded an image into your database and a folder somewhere inside your server. You can use this code to print it out.

<?php
//codes to connect to database and mysql query to get image name from table inside database...

$target_dir = "uploads/"; //path of where the images are store on your server
$image_name = $row_image['image_name']; //name of the image retrieved by mysql query

                //print the image
echo "<img src=" . "'" . $target_dir . "/" . $image_name . "'" . "style='width:50px;height:50px'/>";
?>
smuhero
  • 19
  • 10
  • hey i get this error Notice: Undefined variable: row_image in C:\xampp\htdocs\LovelyHome\bilderladen.php on line 6 what can i do ? – DaviDave Jul 07 '15 at 11:21
  • That is because you need to set the variable like this. $sql= "SELECT * FROM images WHERE image_name = '$search_name'"; $results = mysqli_query( $connection, $sql ); $row_image = mysqli_fetch_assoc($results); – smuhero Jul 07 '15 at 23:00