Forum Moderators: coopster

Message Too Old, No Replies

php not displaying product info after passing in product id

         

nh02173

4:32 pm on May 9, 2011 (gmt 0)

10+ Year Member



hello i am really needing some help with this code i have taken out the html and just left the php code so it will be easier to read. here's the problem after passing in the product id from category to product the final page only displays the product id and not the information to go along with the product (name, price, description, etc)

i have been googling and looking through tutorials and i am just not getting it.

here is the first file final.php



<?php
error_reporting(0);
?>

<html>
<head>
</head>
<body>


<?php



$usr = "root";
$pwd = "";
$db = "mymarket";
$host = "localhost";

//connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}
else



// setup SQL statement
$SQL = " SELECT * FROM categories ";


// execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid);

// check for errors
if (!$retid) { echo( mysql_error()); }
else {



?>

<?php
// display results

while ($row = mysql_fetch_array($retid)) {

$name = $row["name"];
$id = $row['id'];


echo ("<dd><a href=category1.php?id=\"$id\">$name</a></dd>\n<br>");



}
echo ("</dt></p>");

}
?>

</body>
</html>




and the second file category1.php



<?php
error_reporting(0);
?>

<html>
<head>
</head>
<body>

<?php



$usr = "root";
$pwd = "";
$db = "mymarket";
$host = "localhost";

//connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}


$category = "";

// setup SQL statement
$SQL = " SELECT * FROM products ";




?>

<?php

// execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid);

// check for errors
if (!$retid) { echo( mysql_error()); }
else {

// display results
echo ("<p><dt><b>$category</b><br>\n");
while ($row = mysql_fetch_array($retid)) {

$description = $row["description"];
$id = $row["id"];
$name = $row["name"];


echo ("<dd><a href=phones2.php?id=\"$id\">$name</a></dd>\n<br>");
}
echo ("</dt></p>");

}

?>



and the last file the one that only displays the product id (EX: "1" or "2") when i need it to display all of the product information from the id i am passing in.



<?php
error_reporting(0);
?>

<html>
<head>

</head>
<body>

<?php



$usr = "root";
$pwd = "";
$db = "mymarket";
$host = "localhost";

//connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");}



// setup SQL statement
$id=$_GET['id'];
$SQL = "SELECT * FROM products ";

?>

<?php

// execute SQL statement
$retid = mysql_query($db, $SQL, $cid);



// display results
echo "<p><dt><b>$id</b><br>\n";
echo "<p><dt><b>$name</b><br>\n";

while ($row = mysql_fetch_object($retid)) {
$name = $row->name;
$description = $row->description;
$price = $row->price;
echo ("<dd>$name</dd>\n<br>$description<br>$price");
}
echo ("</dt></p>");



?>

</body>
</html>



i know i have some other problems with this code but i thinkl i can work those out i have been stuck on this problem for way to long for a problem that is probably a quick fix.
i know the spacing will be off but my tables are

categories

id | parent_id | name | description

1 | 0 | Apple | Apple phones for ATT
2 | 0 | HTC | HTC phones for ATT
3 | 0 | Samsung | Samsung phones for ATT


and products


id | name | description | price

1 | Iphone 2G | apple smartphone | 129.99
2 | Iphone 3G | apple smartphone | 199.99
3 | Iphone 3GS | apple smartphone | 279.99
4 | Iphone 4 | apple smartphone | 499.99


thank you in advance for any help you can offer me

nh02173

5:13 pm on May 9, 2011 (gmt 0)

10+ Year Member



is my code beyond help?

jspeed

4:44 pm on May 10, 2011 (gmt 0)

10+ Year Member



Your structure is a bit off. On the last page, or "details" pages, you are trying to echo out variables from a "Select all" SQL statement. Change your query to:

$SQL = "SELECT * FROM `products` WHERE `id`= $id";

Also, you cannot echo out the $name variable before you declare what it is. You either need to pass the variable in the url from the previous pages link:

echo ("<dd><a href=phones2.php?id=\"$id\"&name=\"$name\">$name</a></dd>\n<br>");

and then GET it out of the url:

$name=$_GET['name'];

Or place the header of the info table after you have declared the variables out the mySQL table:

while ($row = mysql_fetch_object($retid)) {
$name = $row->name;
$description = $row->description;
$price = $row->price;

// display results
echo "<p><dt><b>$id</b><br>\n";
echo "<p><dt><b>$name</b><br>\n";
echo ("<dd>$name</dd>\n<br>$description<br>$price");
echo ("</dt></p>");
}

rocknbil

4:56 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Patience. :-)

OK so you start here . . .

echo ("<dd><a href=category1.php?id=\"$id\">$name</a></dd>\n<br>");

And assume this "gets" you a link that looks like this.


<dd><a href=category1.php?id="1234">Widget</a></dd>\n<br>

So that should be good. But then in category1.php you do this . . .

$SQL = " SELECT * FROM products ";

Which selects **all** products. You want to add this.

$SQL = " SELECT * FROM products ";
if (isset($_GET['id']) and is_numeric($_GET['id']) and ($_GET['id'] > 0)) {
$SQL .= " where id=" . $_GET['id'];
}

Why the "if?" If ID is not passed it will then display all. A note here, the id field must be a numeric field type for the previous to work; you don't need to quote select statements for numeric fields.

Next,

echo ("<p><dt><b>$category</b><br>\n");

Where are you setting $category? You'd need to get this in a query from somewhere, right? If it's in the current record set, move it below the while loop (which should or could be "if," as below.)

Next,

while ($row = mysql_fetch_array($retid)) {...

If the above works properly, passing ID to the select statement, you should only need "if" here as there is/should be only one record. But taking into the account the previous "if," if ID is not properly passed, you may stick with the while to display multiple records. Which brings us to your question:


while ($row = mysql_fetch_array($retid)) {
$description = $row["description"];
$id = $row["id"];
$name = $row["name"];
$category=$row["category"];
echo ("<p><dt><b>$category</b><br>\n");
echo ("<dd><a href=phones2.php?id=\"$id\">$name</a></dd>\n<br>");
echo ("</dt></p>");
}

All this looks correct, so only one thing I can see: if you're getting "id" (1 and 2 as you say) then you can only have the field names wrong when extracting them from $row. Remember that Name and name, Description and description, and Category/category are all different keys. Check your database field names, I'm guessing you have a case difference.

g1smd

5:10 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ALL of the "connect to database" stuff should be located before the DOCTYPE and <html> tags.

That way, when there is nothing in the database for this request, the script can send the proper HTTP 404 headers.

Otherwise, it is too late to send any HTTP headers once you've started sending the HTML page content.

nh02173

9:28 pm on May 10, 2011 (gmt 0)

10+ Year Member



thank you guys i got it figured out thanks to your help i really appreciate all of your help.