【问题标题】:passed variable returns empty in url on target page传递的变量在目标页面的 url 中返回空
【发布时间】:2015-12-26 08:21:34
【问题描述】:

我快疯了。我正在尝试将一个显示所有专辑缩略图图像和名称的页面中的变量传递到一个页面,该页面将使用该传递的变量显示该画廊中的所有图片,但该变量在目标页面的 url 中为空。我在网上和这个网站上看到过类似的案例,我已经应用了这些建议,但还是一样。这是列出缩略图并传递变量(id)的代码。

    <?php
            include ("config.php");
            $conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD,dbname);
            $albums = mysqli_query($conn,"SELECT * FROM albums");
            if (mysqli_num_rows($albums) == 0) {
                echo "You have no album to display. Please upload an album using the form above to get started. ";
            }
            else{
                echo "Albums created so far:<br><br>";
                echo "<table rows = '4'><tr>";
                while ($thumb = mysqli_fetch_array($albums)) {
                    echo '<td><a href ="view.php?id="'.$thumb['id'].'"/><img src = "'.$thumb['thumbnail'].'"/><br>'.$thumb['album_name'].'<br>'.$thumb['id'].'</a></td>';
                }
                echo "</tr></table>";
            }
     ?>

获取传递变量的代码如下:

<?php
include("config.php");
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
$db = mysqli_select_db($conn,dbname);
if (isset($_GET['id'])) {
    $album_id = $_GET['id'];
    $pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
    $picQuery = mysqli_query($conn,$pic);
    if (!$picQuery) {
        exit();
    }
    if (mysqli_num_rows($picQuery) == 0) {
        echo "Sorry, no Pictures to display for this album";
}
else{
    echo "Pictures in the gallery:<br><br>";
    while ($result = mysqli_fetch_assoc($picQuery)) {
            echo "<img src='".$result['photo_path']."'/>";
        }
    }
}
?>

请帮忙,因为我过去两天一直在努力解决问题。

【问题讨论】:

  • 尝试删除锚点上的双引号。 ..&lt;a href ="view.php?id='.$thumb['id'].'"/&gt;.....
  • 谢谢一百万!!!!!!现在工作得很好。再次感谢。

标签: php mysql sql url


【解决方案1】:

首先,你的代码对 sql 注入很弱:

$album_id = $_GET['id']; // here
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";

使用$album_id = intval($_GET['id']) 或准备好的语句功能。

其次,在代码中添加调试行,例如:

<?php
include("config.php");
if (isset($_GET['id'])) {
    $album_id = intval($_GET['id']);
    var_dump($album_id); // should print actual passed id

    $conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
    var_dump($conn _id); // should print conn resource value

    $db = mysqli_select_db($conn, dbname);
    var_dump($db); // should print 'true' if db select is ok

    $pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
    $picQuery = mysqli_query($conn, $pic);
    var_dump($picQuery); // should print query resource value

    if (!$picQuery) {
        exit();
    }

    if (mysqli_num_rows($picQuery) == 0) {
        echo "Sorry, no Pictures to display for this album";
    } else {
        echo "Pictures in the gallery:<br><br>";
        while (($result = mysqli_fetch_assoc($picQuery)) !== false) {
            var_dump($result ); // should print fetched assoc array
            echo "<img src='".$result['photo_path']."'/>";
        }
    }
}

注意 $album_id = intval($_GET['id'])while (($result = mysqli_fetch_assoc($picQuery)) !== false) 部分

然后点击链接view.php?id=&lt;existing-album-id&gt; 并观察调试结果。在哪一步调试输出与预期不同 - 存在问题。

【讨论】:

    猜你喜欢
    • 2016-02-02
    • 2021-05-20
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    相关资源
    最近更新 更多