2016-04-24 189 views
2

我有两个文件: productlist.php显示所有照片,每个照片都有一个可点击的详细按钮。 productdetail.php显示具有图像和说明的特定照片的详细信息。图片//详细信息不显示

但是,当我点击productlist.php页面中的详细信息按钮时,它将指向productdetail.php页面,但看不到图像或说明。

下面是代码在productlist.php:

<?php 
$link = mysql_connect("xxx", "xxx", "xxx"); 
mysql_select_db("xxx"); 

$sql = "SELECT * FROM products order by createdate desc"; 

$result = mysql_query($sql, $link); 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
    ?> 

    <div class="productlist_content"> 
     <div class="product_list"> 
      <h1><?=$line["name"];?></h1> 
      <img class="product_list_image" 
       src="<?=$line["image"];?>" width="140" height="187"><br><br> 
      <a href="productdetails.php?id=<?=$line["id"];?>"> 
       <img src="images/details.gif" width="60" height="20" border="0"> 
      </a> 
     </div> 
    </div> 
    <?php 
} 
?> 

下面是代码在productdetail.php:

<?php 
if ($id != "") 
{ 
    $link = mysql_connect("xxx", "xxx", "xxx"); 
    mysql_select_db("xxx"); 

    $sql = "SELECT * FROM products WHERE id = '$id'"; 
    $result = mysql_query($sql, $link); 
    $line = mysql_fetch_array($result, MYSQL_ASSOC); 
    $id = $line["id"]; 
    $name = $line["name"]; 
    $description = $line["description"]; 
    $image = $line["image"]; 
} 
?> 

<div class="product_name"><b><?=$name;?></b><br></div> 
<div><img class="product_image" src="<?=$image;?>"> 
    <div class="product_description"> 
     <?=str_replace("\n", "<BR>", $description);?> 
    </div> 
</div> 
<div class="backbutton"> 
    <a href="productlist.php"> 
     <img src="images/btn_back.gif" width="38" height="16" border="0"> 
    </a> 
</div> 
+0

欢迎来到Stack Overflow! PHP中的'mysql_ *'函数已被弃用,不应使用。请阅读[为什么不应该在PHP中使用mysql_ *函数?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)了解有关为什么和用什么来代替它们。 –

回答

0

可以有你的代码,这部分问题:if ($id != "")。您应该从$_GET阵列获得$id。试试这样的:

<?php 
if (!empty($_GET['id']) && $id = $_GET['id']) 
{ 
    $link = mysql_connect("xxx", "xxx", "xxx"); 
    mysql_select_db("xxx"); 

    $sql = "SELECT * FROM products WHERE id = '$id'"; 
    $result = mysql_query($sql, $link); 
    $line = mysql_fetch_array($result, MYSQL_ASSOC); 
    $id = $line["id"]; 
    $name = $line["name"]; 
    $description = $line["description"]; 
    $image = $line["image"]; 
} 
?> 

<div class="product_name"><b><?=$name;?></b><br></div> 
<div><img class="product_image" src="<?=$image;?>"> 
    <div class="product_description"> 
     <?=str_replace("\n", "<BR>", $description);?> 
    </div> 
</div> 
<div class="backbutton"> 
    <a href="productlist.php"> 
     <img src="images/btn_back.gif" width="38" height="16" border="0"> 
    </a> 
</div> 
+1

非常感谢,它真的有效! – Dora