【问题标题】:php: use variable name to display specific image on diffrent pagephp:使用变量名在不同页面上显示特定图像
【发布时间】:2016-06-01 13:22:13
【问题描述】:

这里是 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 html mysql sql


    【解决方案1】:

    不需要表格

    第一个脚本:

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

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

    【讨论】:

    • 我不得不在image_link.php&amp;id 中使用问号而不是和号,我不知道为什么,但它确实有效。谢谢
    • 哦,我的错,是的,它是一个“?” ,我会修改它。
    【解决方案2】:

    将查询更改为:

    $sql_select = "SELECT * FROM images WHERE ID =" $_GET['id'];
    

    【讨论】:

    • 他为什么不能这样学?如果他想这样玩有什么问题??
    • 安全将是主要问题。我没有说答案有什么问题,但这是更好的做法。
    • 同意是的,准备好的声明是最佳做法。
    【解决方案3】:
    echo 
    "<figcaption>
    <a href='image_link.php?id=$table_row[ID]' name='$table_row[ID]'>"
     .$table_row['tytul'] ."
    </a>
    </figcaption>
    </figure></div>";
    

    您需要将 id 作为参数传递到您的 href 中,并以 $table_row[ID] 作为值。

    在 image_link.php 中

    用途:

    $sql_select = "SELECT * FROM images WHERE ID = '$_GET[ID]'"; 
    

    【讨论】:

      【解决方案4】:

      您说要传递图像的 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?id=" . intval($table_row['ID']) . "name=\"" . $table_row['ID'] . "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
          }
      ?>
      

      image_link.php:

      $sql_select = "SELECT * FROM images WHERE ID =" . mysql_real_escape_string($_GET['id']); 
      $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>";
      

      请注意,mysql_* 函数的使用已被高度弃用,您应该改用 mysqli 扩展

      【讨论】:

        猜你喜欢
        • 2014-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2017-03-13
        相关资源
        最近更新 更多