doxd96148 2015-12-26 08:21
浏览 46

传递的变量在目标页面上的url中返回null

I am going out of my mind as it is. I am trying to pass a variable from a page that shows all albums thumbnail image and name to a page that will display all the pictures in that gallery using that passed variable, but the variable is empty in the url on the target page. I have seen similar cases on the web and on this site and I've applied the suggestions but it's still the same. Here is the code that lists the thumbnail and passes the variable(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>";
            }
     ?>

The code for getting the passed variable is as follows:

<?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']."'/>";
        }
    }
}
?>

Please help as i have spent the last two days trying to get it right.

  • 写回答

1条回答 默认 最新

  • dps69208 2015-12-26 08:56
    关注

    First, your's code is weak against sql injections:

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

    Use either $album_id = intval($_GET['id']) or prepared statements functionality.

    Second, add debug lines to your code, like:

    <?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']."'/>";
            }
        }
    }
    

    Notice $album_id = intval($_GET['id']) and while (($result = mysqli_fetch_assoc($picQuery)) !== false) parts

    Then follow link view.php?id=<existing-album-id> and observe debug result. On which step debug output differs from expected - there problem is.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP