Php 从mysql数据库中获取多个图像
我一直在尝试在mysql数据库中插入多个图像。使用下面的代码后,它与其他字段一起作为数组插入到数据库中。但现在我无法获取数组来显示图像Php 从mysql数据库中获取多个图像,php,html,Php,Html,我一直在尝试在mysql数据库中插入多个图像。使用下面的代码后,它与其他字段一起作为数组插入到数据库中。但现在我无法获取数组来显示图像 if(isset($_POST['submit'])){ $title = $_POST['title']; $description = $_POST['description']; $price = $_POST['price']; $days = $_POST['days']; $nights = $_POST['nights']; $people =
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();
}`
我正在使用以下代码获取图像
<img src = "<?php echo $images; ?>>
谁能帮助我,我做错了什么?提前谢谢。以下是变量初始化缺少的代码:
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>
不管上面提到的所有代码如何,但在img标记中,您必须关闭src属性。如果$images变量是array of images,您必须至少执行以下操作
<?php foreach ($images as $image): ?>
<img src="<?= $image ?>">
<?php endforeach ?>
但在您的代码中没有与数据库的通信,没有$images变量初始化,什么都没有。所以我们唯一能做的就是猜测。首先,在>中缺少一个报价。但我看不出该变量是在哪里设置的。您从哪里获取数据也没有数据库代码。给定的代码中没有与数据库的通信。没有插入,就没有选择。请添加相应的代码。我刚刚编辑了我的文章,给出了缺少的代码。我尝试这样做,但遇到了一个错误警告:在第65行的E:\Softwares\xampp\htdocs\Goutam\functions\functions.php中为foreach提供的参数无效。我已经编辑了我的帖子,以澄清变量初始化。我在那部分做了什么错事?谢谢你编辑代码。您可以使用var_dump$图像查看可变内容。在代码中,您正在执行$images=array'images1/。$row['post_images'];-这个数组在这里没用。您只需执行$image='images1/'。$row['post_images'];然后您可以回显$image。若你们在当前代码中有图像,那个么你们需要在上面的答案中做每一件事情。因为如果您回送$images,您将尝试回送整个数组,在本例中它不可用。$images='images1/'.$row['post_images'];….>。。。。。。。即使在这之后,我还是得到了错误警告:为foreach提供的参数无效如果您使用foreach,变量$images必须是array!您现在将其作为字符串,因此没有理由使用foreach。只需创建图像数组并使用foreach,或者创建图像字符串并使用justecho。但不要把这两者结合起来。