0

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!

2
  • Before someone else says it the mysql extension is deprecated. You really need to consider migration to mysqli Commented Dec 16, 2013 at 4:41
  • Yeah, this is for a school project, and is similar to what we've used in this class previously, so I just stuck with it. Commented Dec 16, 2013 at 4:54

2 Answers 2

1

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/ >";
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. I changed that in both cases, didn't seem to affect anything though.
0

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\"";

3 Comments

Ok.I changed my sql statement to include the other fields..makes sense. And my product_id is an int, but thanks. Still not displaying my product info though.
I think you should use the mysqli_fetch_array function instead, and create a while loop to get the contents of the row. Such as, $product_id = $row["product_id"]
Maybe check your database connection details and that the database has rows in its tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.