Jump to content

Recommended Posts

I have to display information about a product ...to do that i have to retrieve the id from the database..

 

<a href="info.php?product_id=<?php echo $row['product_id'];?>">More Info</a>

 

when the user  click on more info it goes to the info page...the product i d is being recognised

like this

 

http://localhost/jacey/info.php?product_id=16

 

but nothing is being displayed in the next page..

 

here is the code for info.php

<?php require_once('../Connections/JaceyConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_info = "-1";
if (isset($_SERVER['product_id'])) {
  $colname_info = $_SERVER['product_id'];
}
mysql_select_db($database_JaceyConn, $JaceyConn);
$query_info = sprintf("SELECT product_id, product_model, product_image, Product_name, product_price, `Description`, products_status FROM product WHERE product_id = %s", GetSQLValueString($colname_info, "int"));
$info = mysql_query($query_info, $JaceyConn) or die(mysql_error());
$row_info = mysql_fetch_assoc($info);
$totalRows_info = mysql_num_rows($info);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="description" content="description"/>
<meta name="keywords" content="keywords"/> 
<meta name="author" content="author"/> 
<link rel="stylesheet" type="text/css" href="bntemplate590/images590/default.css"/>
<title>Jacey</title>
<style type="text/css">
<!--
.style4 {font-size: 14pt}
body,td,th {
font-size: 9pt;
}
-->
</style></head>

<body>

<div class="main">

<div class="main_left">

<div class="header">
		<h1 align="center">Jacey Computers</h1>
  </div>

	<div class="link_menu">
		<a href="main.php" accesskey="1">Home</a>
		<a href="category.php" accesskey="2">Product</a>
		<a href="support.php" accesskey="3">Support & Help</a>
		<a href="contact.php" accesskey="4">Contact Us</a>		</div>

	<div class="content">
		       
      
	 <p> </p>
	 <table border="2">
         <?php  while ($row = mysql_fetch_array($info)){ ?>
		<?php $imageurl = $row['product_image']; ?>

<tr>
<td>
<img src="<?php echo $imageurl;?>" />
    <?php echo $row['Product_name']."<br><font color=red>Price:</font>Rs".$row['product_price'];?>

<form name="form" action="showcart.php" method="POST">
<input name="product_id" type="hidden" value="<?php echo $row['product_id'];?>" />
<input type="submit" value="Add To cart">
<input type="hidden" name="MM_insert" value="form" />
</form>

</td>
</tr>
<?php }?>

   </table>
   <?php echo $row['product_id'];?>
            
  </div>
</div>

</div>
<div class="footer"></div>
</div>
<br style="clear: both;" />

</body>

</html>

 

I am having this same problem evrywhere..i can't add to cart also..

 

 

Link to comment
https://forums.phpfreaks.com/topic/103418-problem-with-retrieving-an-id/
Share on other sites

okay first of all, here's the deal

 

i reckon you're getting an empty page because there's no 'productid' of -1 ... here's what you're doing:

 

$colname_info = "-1";

if (isset($_SERVER['product_id'])) {

  $colname_info = $_SERVER['product_id'];

}

 

You're defining the default, then running a check on a server global variable (which should be get) to see if it's set, but not 'overriding' it if it isn't. This is what i'd do instead:

 

if (!isset($_GET['product_id'])) {

  header('location:back-to-the-previous-page.php');

} else {

  $colname_info = $_GET['product_id'];

}

 

That should help out? I hope ... heheh :)

ok made the changes...

now it looks like that...

<?php require_once('../Connections/JaceyConn.php'); ?>
<?php
$colname_info = (int)$_GET['product_id']

mysql_select_db($database_JaceyConn, $JaceyConn);
$query_info = sprintf("SELECT product_id, product_model, product_image, Product_name, product_price, `Description`, products_status FROM product WHERE product_id = $colname_info" );
$info = mysql_query($query_info, $JaceyConn) or die(mysql_error());
$row_info = mysql_fetch_assoc($info);
$totalRows_info = mysql_num_rows($info);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="description" content="description"/>
<meta name="keywords" content="keywords"/> 
<meta name="author" content="author"/> 
<link rel="stylesheet" type="text/css" href="bntemplate590/images590/default.css"/>
<title>Jacey</title>
<style type="text/css">
<!--
.style4 {font-size: 14pt}
body,td,th {
font-size: 9pt;
}
-->
</style></head>

<body>

<div class="main">

<div class="main_left">

<div class="header">
		<h1 align="center">Jacey Computers</h1>
  </div>

	<div class="link_menu">
		<a href="main.php" accesskey="1">Home</a>
		<a href="category.php" accesskey="2">Product</a>
		<a href="support.php" accesskey="3">Support & Help</a>
		<a href="contact.php" accesskey="4">Contact Us</a>		</div>

	<div class="content">
		       
      
	 <p> </p>
	 <table border="2">
         <?php  while ($row = mysql_fetch_array($info)){ ?>
		<?php $imageurl = $row['product_image']; ?>

<tr>
<td>
<img src="<?php echo $imageurl;?>" />
    <?php echo $row['Product_name']."<br><font color=red>Price:</font>Rs".$row['product_price'];?>

<form name="form" action="showcart.php" method="POST">
<input name="product_id" type="hidden" value="<?php echo $row['product_id'];?>" />
<input type="submit" value="Add To cart">
<input type="hidden" name="MM_insert" value="form" />
</form>

</td>
</tr>
<?php }?>

   </table>
   <?php echo $row['product_id'];?>
            
  </div>
</div>

</div>
<div class="footer"></div>
</div>
<br style="clear: both;" />

</body>

</html>

 

to display from that array..we should use

$row = mysql_fetch_array($id); is this wrong??

 

Because some of my pages are not being displayed and not even retrieving the data from the database...

 

what should i do?

 

Replace the top block of PHP with this for me, and paste in the results :)

 

<?php require_once('../Connections/JaceyConn.php'); ?>

<?php

$colname_info = (int)$_GET['product_id']

 

mysql_select_db($database_JaceyConn, $JaceyConn);

$query_info = sprintf("SELECT product_id, product_model, product_image, Product_name, product_price, `Description`, products_status FROM product WHERE product_id = $colname_info" );

$info = mysql_query($query_info, $JaceyConn) or die(mysql_error());

$row_info = mysql_fetch_assoc($info);

$totalRows_info = mysql_num_rows($info);

 

echo '<pre>';

echo '$query_info = ';

var_dump($query_info)

echo '<br /><br /><br /><br />';

echo '$info = ';

var_dump($info)

echo '<br /><br /><br /><br />';

echo '$row_info= ';

var_dump($row_info)

echo '<br /><br /><br /><br />';

echo '</pre>';

?>

here is the output:

 

$query_info ="SELECT product_id, product_model, product_image, Product_name, product_price, `Description`, products_status FROM product WHERE product_id = $colname_info" string(143) "SELECT product_id, product_model, product_image, Product_name, product_price, `Description`, products_status FROM product WHERE product_id = 16" $info = mysql_query($query_info, $JaceyConn) or die(mysql_error())resource(4) of type (mysql result) $row_info= mysql_fetch_assoc($info)array(7) { ["product_id"]=> string(2) "16" ["product_model"]=> string(4) "H755" ["product_image"]=> string(39) "C:/Apache2.2/htdocs/Prod/Laptop/hp2.jpg" ["Product_name"]=> string(23) "HP Compaq Presario F755" ["product_price"]=> string(5) "50000" ["Description"]=> string(853) "Full Specifications: HP Compaq Presario F755 Brand processor : AMD Processor Class: Turion 64x2 Processor Speed: 1.9 GHz Installed Memory: 2 GB Maximum Memory: 3 GB Memory Technology: DDR II SDRAM Cache Size :1 MB Video Memory: 128 MB Hard Drive Capacity :160 GB Included Drives: DVD±RW (±R DL) / DVD-RAM / HD DVD-ROM CD Write Speed :16 X CD Rewrite Speed: 10 X CD Read Speed: 24 X DVD Read Speed: 8 X DVD Write Speed :4x (DVD±R), 2x (DVD-R DL), 2.4x (DVD+R DL) Display Size: 15.4 in Display Type :TFT active matrix Graphics Processor: Intel GMA X1300 GT Resolution :1280 x 800 pixels Battery Life: 3.0 hrs Battery Type :Lithium ion Wireless Technology :Bluetooth Modem Speed :56 Kbps Software Operating System Microsoft Windows Vista installed Warranty 1 year " ["products_status"]=> string(1) "1" } $totalRows_info = mysql_num_rows($info)int(1)

 

It seems that you have only one row in your table as $totalRows_info is equal to 1 and you already fetched that row before you start your HTML part. After that you make a loop to fetch the next rows and display their info, but there are no more rows.

 

I think you should remove this line from the top part of your code.

$row_info = mysql_fetch_assoc($info);

 

if i remove it then i should only use the array to retrieve the data..like that

 

while($row=mysql_fetch_array($info){

$row['product_id'];

.

.

.}

 

i am having that problem in almost all my pages...nothing is being retrieved from the database n displayed...

 

thnx a lot for ur help..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.