dongqie2028 2016-01-11 17:34
浏览 18

如何从多图像上传显示多个图像

I used the following code to add multiple images to my database through the admin area of my website, this works fine, but I want to display the multiple images that were uploaded for that specific vehicle id... Basically I'm trying to create a website for a car dealer, when the admin is adding stock they can choose multiple images to add to the listing, here is the add stock code

<?php

if(isset($_POST['addstock'])){

$vehicle_make = $_POST['vehicle_make'];
$veh_model = $_POST['veh_model'];
$veh_mileage = $_POST['veh_mileage'];
$veh_description = $_POST['veh_description'];
$veh_price = $_POST['veh_price'];
$veh_gearbox = $_POST['veh_gearbox'];
$veh_engine_size = $_POST['veh_engine_size'];
$veh_fuel_type = $_POST['veh_fuel_type'];

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

$file_name = $key.$_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];


$addstock = "insert into stock (veh_make,veh_model,veh_mileage,veh_description,veh_gearbox,veh_engine_size,veh_fuel_type,veh_price,file_name,file_size,file_type) values('$vehicle_makee','$veh_model','$veh_mileage','$veh_description','$veh_gearbox','$veh_engine_size','$veh_fuel_type','$veh_price','$file_name','$file_size','$file_type')";

        move_uploaded_file($file_tmp,"image_uploads/".$file_name);
        }

$addsto = mysqli_query($con, $addstock);

if($addsto){

    echo "<script>alert('Vehicle has been added')</script>";
    echo "<script>window.open('addstock.php','_self')</script>";
}
}

The code succesfully adds multiple images to the directory folder,

Here is the code outside the admin area, on the vehicle details page, only 1 images displays out of the multiple images I uploaded, I want all images to be displayed that were uploaded for the vehicle ID, eventually this will be styled into an image gallery,

<?php
if(isset($_GET['vehicle_id'])){

$vehicle_id = $_GET['vehicle_id'];

$get_veh = "select * from stock where vehicle_id='$vehicle_id'";

$run_veh = mysqli_query($con, $get_veh);

while($row_veh=mysqli_fetch_array($run_veh)){

    $vehicle_id = $row_veh['vehicle_id'];
    $veh_make = $row_veh['veh_make'];
    $veh_model = $row_veh['veh_model'];
    $veh_mileage = $row_veh['veh_mileage'];
    $veh_price = $row_veh['veh_price'];
    $veh_gearbox = $row_veh['veh_gearbox'];
    $veh_description = $row_veh['veh_description'];
    $file_name = $row_veh['file_name'];

    echo "

        <div id='single_vehicle'>


            <div id='box1'>$veh_make $veh_model</div>
            <div id='box3'>£$veh_price</div>
        </div>


        <div id='single_vehicle2'>

            <div id='box2'><img src='admin/image_uploads/$file_name' width='600' height='450' /></div>
            <div id='box4'>
            <div id='clickbox1'><a href='#'>Book Test Drive</a></div>
            <div id='clickbox1'><a href='#'>Send Enquiery</a></div>
            <div id='clickbox1'><a href='#'>Print this Page</a></div>
            <div id='clickbox1'><a href='#'>Email this Page</a></div>

            </div>



        </div>

        <div id='desription_area'>
        <div id='desription_area1'><span style='text-decoration:underline'>Vehicle Description</span><br>$veh_description</div>
        <div id='desription_area2'><span style='text-decoration:underline'>Specification</span><br>Make: $veh_make<br>$veh_model<br>$veh_mileage</div>
        </div>

        ";
}
}
?>

I'm new to coding and still learning, what have I done wrong? Any help is much appreciated

  • 写回答

1条回答 默认 最新

  • doupai1876 2016-01-11 20:14
    关注

    hi maybe you have a error on you code please try this and check your code

    for upload image

    <?php
    if(count($_FILES) > 0) {
    if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
    mysql_connect("localhost", "root", "");
    mysql_select_db ("phppot_examples");
    $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
    $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
    $sql = "INSERT INTO output_images(imageType ,imageData)
    VALUES('{$imageProperties['mime']}', '{$imgData}')";
    $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
    if(isset($current_id)) {
    header("Location: listImages.php");
    }}}
    ?>
    <HTML>
    <HEAD>
    <TITLE>Upload Image to MySQL BLOB</TITLE>
    <link href="imageStyles.css" rel="stylesheet" type="text/css" />
    </HEAD>
    <BODY>
    <form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
    <label>Upload Image File:</label><br/>
    <input name="userImage" type="file" class="inputFile" />
    <input type="submit" value="Submit" class="btnSubmit" />
    </form>
    </div>
    </BODY>
    </HTML>
    

    and for show the image save on database

    <?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("phppot_examples") or die(mysql_error());
    if(isset($_GET['image_id'])) {
    $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
    $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
    $row = mysql_fetch_array($result);
    header("Content-type: " . $row["imageType"]);
    echo $row["imageData"];
    }
    mysql_close($conn);
    ?>
    

    for list imagens

    <?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("phppot_examples");
    $sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
    $result = mysql_query($sql);
    ?>
    <HTML>
    <HEAD>
    <TITLE>List BLOB Images</TITLE>
    <link href="imageStyles.css" rel="stylesheet" type="text/css" />
    </HEAD>
    <BODY>
    <?php
    while($row = mysql_fetch_array($result)) {
    ?>
    <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
    <?php       
    }
    mysql_close($conn);
    ?>
    </BODY>
    </HTML>
    

    For displaying BLOB images to the browser, we have to create a PHP file to do to following.

    get image data and type from MySQL BLOB Set the content-type as image (image/jpg, image/gif, …) using PHP header(). print image content.

    good luck and try

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改