如何使用动态页面的索引号链接到页面


How to Link to a page using index number for a dynamic page

我用以下代码从数据库创建了一个页面:

<?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的页面,等等其他链接?我真希望我把问题解释清楚了。

我希望我理解这个问题,但它应该工作,如果你用一个参数代替prod_id = 1。

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

然后从URL参数($_GET/URL解析)中创建$postID。

示例: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注入,关心安全问题是非常重要的)