tags:

views:

58

answers:

2

i am fairly new to php and mysql and was incidentally bugfixing a project a friend of mine had done. let me get straight to it.

this is a php file that cretaes a temp db:

<?php
include("config.php");

//define the temp table
$query = "CREATE TABLE carttemp(
          carttemp_hidden INT(10) NOT NULL AUTO_INCREMENT,
       carttemp_sess CHAR(50)  NOT NULL,
       carttemp_prodnum CHAR(5) NOT NULL,
       carttemp_quan INT(3) NOT NULL,
       PRIMARY KEY (carttemp_hidden),
       KEY(carttemp_sess))";

$temp = mysql_query($query) or die(mysql_error());
echo "Temporary cart table created successfully!";
?>

this is a fragment of the html file that is trying to access this table:

<table border="1" align="left" cellpadding="5">
  <tr>
     <td>Quantity</td>
     <td>Item Name</td>
     <td>Price Each</td>
     <td>Extended Price</td>
     <td></td>
     <td></td>
  </tr>  

<?php

$_sessid = session_id();

$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query) or die (mysql_error());

$total = 0;
while($row = mysql_fetch_array($results))
{
extract($row);
$prod = "SELECT * FROM product WHERE product_prodnum = '$carttemp_prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract($prod3);

echo "<tr><td>";
echo $carttemp_quan;
echo "</td>";
echo "<td>";
echo "<a href=\"getprod.php?prodid=" . $product_prodnum . "\">";
echo $product_name;
echo "</td></a>";
echo "<td align=\"right\">";
echo $product_price;
echo "</td>";
echo "<td align=\"right\">";

//get extended price
$extprice = number_format($product_price * $carttemp_quan,2);
echo $extprice;
echo "</td>";
echo "<td>";
echo "<a href=\"cart.php\">Make changes to Cart.</a>";
echo "</td>";
echo "</tr>";

//add extended price to total
$total = $extprice + $total;
}
?>
<tr>
<td colspan="4" align="right">Your total before shipping is :</td>
<td align="right"> <?php echo number_format($total, 2); ?></td>
<td></td>
<td></td>
</tr>
</table>

the thing is i am unable to display the table details in the above code. can anyone please help?

A: 

You should install and use a debugger, e.g. xdebug and netbeans as ide.
Until then you can use an echo/printf debugger, i.e. print some more debugging information like the actual query sent to mysql, the number of results, ....
(Don't forget to remove the debug output or make it conditional in some way.)

You should also look into sql joins.
Instead of extract($row)/$carttemp\_prodnum you could simply use $row['carttemp\_prodnum'] without "polluting" namespace.
SELECT carttemp\_prodnum,x,y,z instead of SELECT * would also be nice.

$_sessid = session_id();
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query) or die (mysql_error());

echo '<pre>Debug: ', __FILE__, ' ', __LINE__, '
  $_sessid=', htmlspecialchars($_sessid), '
  $query=', htmlspecialchars($query), '
  num($results)=', mysql_num_results($results), '
</pre>';
?>
<table border="1" align="left" cellpadding="5">
  <tr>
     <td>Quantity</td>
     <td>Item Name</td>
     <td>Price Each</td>
     <td>Extended Price</td>
     <td></td>
     <td></td>
  </tr>  
<?php
$total = 0;
while($row = mysql_fetch_array($results))

...

VolkerK
+2  A: 

There is a typo in your code:

<?php

$_sessid = session_id();

$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";

You are saving the session ID in variable $_sessid but use $sessid without the underscore in your SQL.

Ferdinand Beyer
@Ferdinand - Wow! Nice catch.
Anthony
@amit - You would have known this was where the problem was if you accounted for 0 results returned from the query. For each of your queries, you should have something that handles an empty result set. Since the syntax of the query was correct, there was no error, which is how you missed it. No results is not the same as query error. So for each query, you need to add something like `if( mysql_num_rows($results) < 1) {echo "Nothing found!";}`. I suggest the output having a keyword which indicates what wasn't found, so you know which of the queries failed.
Anthony
thank you so much. thank you.
amit