简体   繁体   中英

fetching multiple images from mysql database

I have been trying to insert multiple images in mysql database. After using below code, it was inserted in the database along with other fields as array. But now I am unable to fetch array to display images.

if(isset($_POST['submit'])){
$title = $_POST['title'];

$description = $_POST['description'];

$price = $_POST['price'];
$days = $_POST['days'];
$nights = $_POST['nights'];
$people = $_POST['people'];
$hotel_rating = $_POST['hotel_rating'];
$images_name = $_FILES['images']['name'];
$images_type = $_FILES['images']['type'];
$images_size = $_FILES['images']['size'];
$images_tmp = $_FILES['images']['tmp_name'];
$inclusion = $_POST['inclusion'];
$exclusion = $_POST['exclusion'];
$date = date('d/m/y');
$category = $_POST['category'];


if($title == '' or $category == '' or $description == '' or $price == '' or $days == '' or $nights =='' or $people =='' or $hotel_rating==''
or $images_name == '' or $inclusion =='' or $exclusion== ''){
    echo "<script>alert('Please fill all fields!')</script>";
    exit();
}
    for($i = 0; $i< count($images_tmp)-1; $i++){
        if($images_type[$i] == "image/jpeg" or $images_type[$i] == "image/png" or $images_type[$i] == "image/gif"){
    if($images_size[$i] <= 150000 && $images_size[$i] >= 100000){
        move_uploaded_file($images_tmp[$i], "images1/".$images_name[$i]);
        }
    else {
        echo "<script>alert('Please upload image between 100kb and 150kb only.')</script>";
        exit();
    }
    }
else{
echo "<script>alert('Image type is invalid!')</script>";
exit();
}`

I am using the following code to fetch images.

<img src = "<?php echo $images; ?>> 

Can anyone help me that what am I doing wrong? Thanks in advance. Following is the code missing for variable initialization:

    if(!isset($_GET['cat'])){
$query = "select * from post order by 1 DESC LIMIT 0,4";
$run = mysqli_query($db, $query);
while($row = mysqli_fetch_array($run)){
    $post_id = $row['post_id'];
    $title = $row['post_title'];
    $description = substr($row['post_desc'], 0, 300);
    $price = $row['post_price'];
    $days = $row['post_days'];
    $nights = $row['post_nights'];
    $people = $row['post_people'];
    $hotel_rating = $row['post_hotel'];
    $images = array('images1/'.$row['post_images']);
    $inclusion = $row['post_include'];
    $exclusion = $row['post_ninclude'];
    $date = $row['post_date']; ?>       
    <!-- Item -->
    <div class="item clearfix rating_5">
    <div class="item_image"><img src = "<?php echo $images; ?>" width="500" height="400" style="width:100%; height:400px; object-fit: cover;" alt="Image cannot be displayed"></div>

If the $images variable is array of images, you have to do at least

<?php foreach ($images as $image): ?>
    <img src="<?= $image ?>">
<?php endforeach ?>

But in your code is no communication with database, no $images variable initialization, nothing. So only we can do is guess.

regardless for all of the mentioned code but in your img tag you have to close the src attribute.

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