如何从 mysql 数据库中的图像创建图像幻灯片

How to create an image slideshow from images in a mysql database

本文关键字:图像 创建 幻灯片 数据库 mysql      更新时间:2023-09-26

我有一个mysqli数据库和表单,允许我存储ID,名称和照片。照片的路径设置为服务器上的"图像"文件夹。我有一个查询可以

从名称 = $pagetitle 的图像中选择 *。

这绝对可以正常工作,在javascript幻灯片之外。当我在 javascript 中放置一个 php 命令来查找要显示的图像时,js 只显示 1 张图像,而不是所有图像。

任何帮助将不胜感激,谢谢。

有问题的代码部分如下...

索引.php

            <!-- Image Slide Show Start -->
  <div style="display: flex; justify-content: center;">
  <img align="middle" src="" name="slide" border=0 width=300 height=375>
				
<script>
<?php 
require('dbconnect.php');
$data = mysql_query("SELECT * FROM images WHERE name= '$pagetitle'");
$image = mysql_fetch_array( $data );
?>
//configure the paths of the images, plus corresponding target links
slideshowimages("<?php echo "/images/".$image['photo'] . ""?>")
//configure the speed of the slideshow, in miliseconds
var slideshowspeed=2000
var whichlink=0
var whichimage=0
function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage
if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}
slideit()
</script> </div><br><br>
						<!-- Image Slide Show End -->

更新查询有语法错误,在字段之间使用,,还必须包含 2 个'中的字符串:

$query = "UPDATE page_content SET PageTitle='$pageTitle',
PageContent='$PageContent', PageContent2='$PageContent2' WHERE PageId='$PageId'";

您错过了字段之间的,以及查询中变量周围的''

$sql = "UPDATE page_content SET PageTitle='$pageTitle', 
       PageContent='$PageContent', PageContent2='$PageContent2' 
       WHERE PageId='$PageId'";
// check query executed successfully or get error
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));

$result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);

希望它能帮助你:)

试试这个:

$sql = "SELECT * FROM images WHERE name= '$pagetitle'";
$result = $conn->query($sql);
$directory = '';
while( $image = $result->fetch_assoc() )
    $directory .= ($directory != '' ? "," : '') . ('"/images/'.$image["photo"] . '"');

// Check if it was successfull
    if($directory != '') {
    // if there are images for this page, run the javascript
    ?><script>

    //configure the paths of the images, plus corresponding target links            
    slideshowimages(<?php print $directory ?>)