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

时间:2014-08-03 17:08:52

标签: php mysql dynamic hyperlink indexing

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

<?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等等其他链接? 我真的希望我能很好地解释我的问题。

1 个答案:

答案 0 :(得分:0)

我希望我能理解这个问题,但是如果用参数替换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注入,那么关心安全问题非常重要)

祝你好运!