Andy Olle
Andy Olle

Reputation: 7

Displaying info from a sql database issue

I'm making a simple shopping cart application. The main page displays a list of products.When you click on the name of the product, it should take you to a page where just that product info is displayed. Right now, it's not displaying anything. Here's my code:

<?php

    $sql = "SELECT product_id, image_path, name, description, price FROM products;";
    $result = mysql_query($sql);
    while(list($product_id, $image_path, $name, $description, $price) = mysql_fetch_row($result)) {

        echo "<tr>";
            echo "<td><img src=$image_path height=200 width=160 border=1px /></td>";
            echo "<td><b><a href=\"getprod.php?product=" .
                  $product_id['product_id'] . "\">$name</a></b><br/ >";
            echo "$description<br/ >";
            echo "$$price<br/ >";
            echo "<input type='button' value='Add to Cart' onclick='addtocart'/>"; 

        echo "</tr>";

    }

?>

And my productInfo method that is supposed to display my info:

function productInfo($product_id) {
    global $conn;

    if($product_id) {
        $sql= "SELECT product_id,image_path, name, description, price " . 
              "FROM products WHERE product_id = " . $product_id; " . 

        $result= mysql_query($sql, $conn) or
            die('Could not find product info: ' . mysql_error());

        while(list($product_id, $image_path, $name, $description, $price) = mysql_fetch_row($result)) {
            echo "<td><img src=$image_path height=200 width=160 border=1px /></td>";
            echo "<td><b><a href=\"getprod.php?product=" .
                  $product_id['product_id'] . "\">$name</a></b><br/ >";
            echo "$long_description<br/ >";
            echo "$$price<br/ >";
            echo "<input type='button' value='Add to Cart' onclick='addtocart'/>"; 
        }

        }
    }

Any help or nudge in the right direction would be greatly appreciated!

Upvotes: 0

Views: 57

Answers (2)

SyntaxLAMP
SyntaxLAMP

Reputation: 985

You're only selecting the "product_id" from the database table. You should specify what you want to return if you are going to use the list() command:

$sql= "SELECT product_id, image_path, name, description, price FROM products WHERE product_id = " . $product_id;

Also, if your product_id is not listed as an Integer in the database, and is instead text or varchar, you may want to put quotes around the product id

$sql= "SELECT product_id, image_path, name, description, price FROM products WHERE product_id = \"$product_id\"";

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

as you are fetching $product_id with list(), in your while loop, change:

echo "<td><b><a href=\"getprod.php?product=" .
                  $product_id['product_id'] . "\">$name</a></b><br/ >";

to

echo "<td><b><a href=\"getprod.php?product=" .
                  $product_id . "\">$name</a></b><br/ >";

Upvotes: 1

Related Questions