简体   繁体   中英

Display all images from database (php)

I use this code to upload images in the database:

<?php

//Store the upload form
$UploadForm = " <form id='idForm' action='upload.php' method='post' enctype='multipart/form-data'>
                    <input type='file' name='image'/><br/><br/>
                    <input id='BTN' type='submit' value='Upload'/><br/><br/>
            </form>";
//if logged in show the upload form
if($userid && $username){
echo $UploadForm;
// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//file properties
if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

//if image selected
if(isset($file) && $file != ""){
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

if($image_size == FALSE){
    echo "That's not an image!";
    header( "refresh:2;url=upload.php" );
}
else{
    $qry = mysqli_query($con,"SELECT * FROM store WHERE name='$image_name'");
    $Nrows = $qry->num_rows;
    if( $Nrows == 0){
            if(!$insert = mysqli_query($con,"INSERT INTO store VALUES         ('','$image_name','$username','$image')")){
            echo "We had problems uploading your file!";
            header( "refresh:2;url=upload.php" );
        }
        else{
            echo "Image $image_name uploaded!";
            header( "refresh:2;url=upload.php" );
        }
    }
    else{
        echo "There is already an image uploaded with the name $image_name<br/>";
    }
}
}   
else{
    echo "Please select an image";
}
mysqli_close($con);
}
else{
    echo "You have to be logged in to upload!";
}
?>

And this code to display all images from the database:

// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$res = mysqli_query($con,'SELECT * FROM store');
while($row = $res->fetch_array()){
    $image = $rows['image'];
    echo "<img src='".$image."' />";
}

And I get something like tons of random symbols like diamonds with question marks in them and letters instead of my image. The scripts are not made by me. I just watched some tutorials and combined them and it seems that I didn't "combined" them properly. What am I doing wrong?

LATER EDIT:

HTML:

<img src="getImage.php?id=26"/>

PHP (getImage.php):

$con = mysqli_connect('***', '***', '***', '***_dbimage');
if(isset($_GET['id']))
{
    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query("SELECT * FROM store WHERE id=$id");
    while($row = mysql_fetch_assoc($query))
    {
        $imageData = $row['image'];
    }
    header("content-type:image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

Still can't get it to work! Help please!

I finaly did it! This is the upload script:

<?php

//Store the upload form
$UploadForm = " <form id='idForm' action='upload.php' method='post' enctype='multipart/form-data'>
                    <input type='file' name='image'/><br/><br/>
                    <input id='BTN' type='submit' value='Upload'/><br/><br/>
            </form>";
//if logged in show the upload form
if($userid && $username){
    echo $UploadForm;
// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//file properties
if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

//if image selected
if(isset($file) && $file != ""){
    $image = mysqli_real_escape_string($con,file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size == FALSE){
        echo "That's not an image!";
        header( "refresh:2;url=upload.php" );
    }
    else{
        $qry = mysqli_query($con,"SELECT * FROM store WHERE name='$image_name'");
        $Nrows = $qry->num_rows;
        if( $Nrows == 0){
            if(!$insert = mysqli_query($con,"INSERT INTO store VALUES     ('','$image_name','$username','$image')")){
            echo "We had problems uploading your file!";
            header( "refresh:2;url=upload.php" );
        }
        else{
            echo "Image $image_name uploaded!";
            header( "refresh:2;url=upload.php" );
        }
    }
    else{
        echo "There is already an image uploaded with the name $image_name<br/>";
    }
}
}   
else{
    echo "Please select an image";
}
mysqli_close($con);
}
else{
    echo "You have to be logged in to upload!";
}
?>

Here is the diplay script:

$con = mysqli_connect('***', '***', '***', '***_dbimage');
$query = mysqli_query($con,"SELECT id FROM store");
while($row = mysqli_fetch_assoc($query))
{
    $IDstore = $row['id'];
    echo "<img src='getImage.php?id=".$IDstore."'/>";
}

And the "getImage.php":

<?php

$con = mysqli_connect('***', '***', '***', '***_dbimage');
if(isset($_GET['id']))
{
    $id = mysqli_real_escape_string($con,$_GET['id']);
    $query = mysqli_query($con,"SELECT * FROM store WHERE id=$id");
    while($row = mysqli_fetch_assoc($query))
    {
        $imageData = $row['image'];
    }
    header("content-type:image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

I hope it will help someone cause it's ready to use now. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM