URL中显示的下一个和上一个链接和ID用于共享

时间:2013-06-19 05:25:28

标签: php html mysql sql button

我有一个已设置好几张图片的数据库,我想让网址在用户点击下一步时显示每个查询的ID。用户应该能够共享URL并将其粘贴到浏览器中,URL应该从查询中提取该唯一ID。我遇到的问题是每次粘贴网址时,我都会得到一张随机图片而不是ID中的图片。我在这里不知所措,我不知道该怎么做:(这里是我到目前为止的代码。

<?php
if (isset($_GET['id'])) {
    include("PHP/db.php");

    echo $where = $_GET["id"];
    echo $query = "SELECT * FROM images WHERE ID =" . $where;

    $result = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($result);
    $ID = $row['ID'];
    $title = $row['name'];

    $image =  "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}

if($_GET['next4']) {
    echo 'HELLO THIS IS THE NEXT IF METHOD';

    include("PHP/db.php");

    $query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
    $result = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($result);
    $ID = $row['ID'];
    $title = $row['name'];

    $image =  "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
} 

?>
<body>
</div>
    <div id="title"> <?php echo $title ?> </div>

    <div id="mainpic">
    <?php echo $image ?>
    </div>

    <div id="prevnext">
        <div id="next">
        <a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
        </div>

        <div id="prev">
            <a href="?id=<?php echo $ID ?>">Previous</a>
        </div>
    </div>

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php
include("PHP/db.php");

$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];

if (!isset($_GET['id'])) {
    $_GET['id'] = $rand_id;
}

if (isset($_GET['id'])) {
    echo $where = $_GET["id"];
    echo $query = "SELECT * FROM images WHERE ID =" . $where;

    $result = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($result);
    $ID = $row['ID'];
    $title = $row['name'];
    $image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>

<body>
</div>
    <div id="title"> <?php echo $title ?> </div>

    <div id="mainpic">
    <?php echo $image ?>
    </div>

    <div id="prevnext">
        <div id="next">
            <a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
        </div>

        <div id="prev">
            <a href="?id=rand">Previous</a>
        </div>
    </div>

我改变了:

-link to next现在是id = rand
- 改变你的代码给我一个“兰特ID”,它已经在你加载的页面的href上定义了

答案 1 :(得分:1)

它将进入条件(next == true)

确保在使用前初始化变量。