php mysql tabloları almak [kapalı]

2 Cevap php

PHP ve MySQL için oldukça yeni ve tesadüfen bir arkadaşım yapmıştı bir proje arıza giderme oldu. bana bu düz başlayalım.

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>

şey i yukarıdaki kodu tablo bilgilerini görüntülemek mümkün duyuyorum. Herkes lütfen yardım edebilir?

2 Cevap

Kodunuzda bir yazım hatası vardır:

<?php

$_sessid = session_id();

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

Siz değişken olarak oturum kimliği tasarrufu $_sessid ama SQL çizgi olmadan $sessid kullanırlar.

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))

...