gpt4 book ai didi

php - 如何显示多张图片上传中的多张图片

转载 作者:行者123 更新时间:2023-11-29 21:38:27 25 4
gpt4 key购买 nike

我使用以下代码通过网站的管理区域将多个图像添加到我的数据库中,这工作正常,但我想显示为该特定车辆 ID 上传的多个图像...基本上我是尝试为汽车经销商创建一个网站,当管理员添加库存时,他们可以选择多个图像添加到列表中,这是添加库存代码

<?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>";
}
}

代码成功将多张图片添加到目录文件夹中,

这是管理区域外的代码,在车辆详细信息页面上,我上传的多张图片中仅显示 1 张图片,我希望显示为车辆 ID 上传的所有图片,最终将设置样式进入图片库,

<?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 Enquiry</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>

";
}
}
?>

我是编码新手,仍在学习中,我做错了什么?非常感谢任何帮助

最佳答案

嗨,也许您的代码有错误,请尝试此操作并检查您的代码

用于上传图片

<?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>

并显示保存在数据库中的图像

<?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);
?>

用于列表图像

<?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>

为了向浏览器显示 BLOB 图像,我们必须创建一个 PHP 文件来执行以下操作。

从 MySQL BLOB 获取图像数据和类型 使用 PHP header() 将内容类型设置为图像(image/jpg、image/gif...)。打印图像内容。

祝你好运,尝试一下

关于php - 如何显示多张图片上传中的多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com