php:使用变量名在不同页面上显示特定图像

标签 php html mysql sql

这里是 Php 初学者。我在页面上有 php 循环来显示图像(基本上是一个画廊),每个图像都有一个标题显示在下面。这些标题是指向仅显示一张图像的第二页的链接。 问题是每个链接都有不同的名称(名称的值是图像的特定/ID)。

<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC"; 
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
    echo "<div class=\"galery-1-3\"><figure>";
    echo "<img src=\"" . $table_row['file_path'] . "\" />";
    echo "<figcaption><a href=image_link.php name=\"" . $table_row['ID'] . "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
    }
?>

我正在显示一张图片,但无论我点击什么标题,我总是从表格中获取最后一张图片。我如何才能在 image_link.php 上捕获特定 ID,或者我应该使用某种灵活的地址,如 /image_link.php?id=34。我不知道从哪里开始。

image_link.php 包含基本相同的代码,但没有循环:

$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID"; 
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
    echo "<div class=\"galery-big\"><figure>";
    echo "<img src=\"" . $table_row['file_path'] . "\" />";
    echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";

在此先感谢您的帮助。

最佳答案

不需要表格

第一个脚本:

<?php
$sql_select = "SELECT * FROM images ORDER BY data DESC"; 
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
    echo "<div class=\"galery-1-3\"><figure>";
    echo "<img src=\"" . $table_row['file_path'] . "\" />";
    echo "<figcaption><a href=\"image_link.php?id=" . $table_row['ID'] 
     . "\" name=\"" . $table_row['ID']
     . "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>

第二个脚本

$SomehowGetID=$_GET['id'];
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID"; 
$sql_table_selected = mysql_query($sql_select); 
$table_row = mysql_fetch_array($sql_table_selected);
    echo "<div class=\"galery-big\"><figure>";
    echo "<img src=\"" . $table_row['file_path'] . "\" />";
    echo "<figcaption>" . $table_row['tytul'] 
    . "</figcaption></figure>   </div>";

尽管我建议您使用准备好的语句而不是简单的查询结构。

关于php:使用变量名在不同页面上显示特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37569993/

相关文章:

php - 准备好的语句中的 Pdo 参数错误

javascript - 三个依赖下拉菜单问题

javascript - 待办事项应用程序没有显示错误,但 src .js 文件不会显示

php - 提交到 SQL 后更新文本区域

php - 解析错误 : syntax error, unexpected T_ELSE, expecting T_FUNCTION in/home/petit/public_html/system/database/mysqli.php on line 54

php - 使用 php 显示 Mysql 表中的图表

javascript - 我怎么能: Select a paragraph with JavaScript (on click)?

php - 如何使用codeigniter在两个表中获取数据菜单和子菜单

python - Django 1.5 DatabaseError at/(1146, "Table ' x.y'不存在”)

php - 创建声音-读取字母-在PHP中