2

I have two files : productlist.php shows all photos each with a detail button that you can click. productdetail.php shows details of one specific photo with image and description.

However, when I click the detail button in productlist.php page, it directs to productdetail.php page but cannot see image or description.

Below are codes in 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
}
?>

Below are codes in 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>
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Dora
  • 23
  • 4
  • Welcome to Stack Overflow! The `mysql_*` functions in PHP are deprecated and shouldn't be used. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines Apr 24 '16 at 09:51

1 Answers1

0

There can be problem with this part of your code: if ( $id != "" ). You should get $id from $_GET array. Try soomething like this:

<?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>
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46