【问题标题】:How to Link to a page using index number for a dynamic page如何使用动态页面的索引号链接到页面
【发布时间】:2014-09-26 05:45:17
【问题描述】:

我从数据库中创建了一个页面,代码如下:

<?php
$sql = "SELECT * FROM `inventory` where `prod_id` = 1"; // manipulate id ok 
$qry = mysql_query($sql);
$result=mysql_fetch_array($qry);

// this is code to display picture
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['prod_pic'] ).'"/>'

?>
<br>

<?php
// this is to display info

    $qry = "SELECT * from inventory where prod_id = 1";
    $result = @mysql_query($qry);
    while ($row=mysql_fetch_assoc($result)){
        echo $row['prod_desc'];
    }
?>

现在我的问题或疑问是如何创建一个链接来替换 prod_id 的 id,例如 sample1 链接包含 prod_id 编号 1,我如何制作一个动态页面,以便当我按下 sample1 时它会转到该页面产品编号 1 等等其他链接? 我真的希望我能很好地解释我的问题。

【问题讨论】:

    标签: php mysql dynamic hyperlink indexing


    【解决方案1】:

    我希望我能理解这个问题,但如果您将 prod_id = 1 替换为参数,它应该可以工作。

    $qry = "SELECT * from inventory where prod_id = ".$prodID;
    

    然后你从 URL 参数创建 $postID ($_GET / URL Parsing)。

    http://yourdomain.com/sample?product=1 为例

    $prodID = your_escape_function($_GET['product']);
    

    为避免一些安全问题,请在对数据库执行查询之前转义 $prodID 变量。


    编辑:要创建链接,请在基础中选择每个产品。

    $qry = "SELECT * from inventory where prod_id;
    

    然后为每个产品创建你的 URL,(我有一段时间没有使用 mysql_fetch,代码可能是错误的,但想法很好):

    $result = @mysql_query($qry);
    while ($row=mysql_fetch_assoc($result)){
        echo '<a href="http://yourdomain.com/sample?product='.$row['prod_id'].'">Link to product #'.$row['prod_id'].'</a>';
    }
    

    (在这种情况下,如果您将网站置于在线/可访问状态,特别是在这种情况下,关注安全问题非常重要,尤其是 sql 注入)

    祝你好运!

    【讨论】:

    • 那么如何为参数创建链接?我对php真的很陌生,我的论文真的让我很生气。谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多