Retrieving DATA from MySQL DATABASE

A

Anonymous

Guest
Hi all,

Please I need some help here. I'm retrieving data from mysql table "bookcopy" and displaying list of books with their prices. I want users to be able to view details of any book they clicked on, on a page I called detail.php.

The list is displayed successfully, how ever, if you click any of the books, it shows the details of the first book on the list, on matter which one you select. Can anyone help me out please?

Below is the code I used. I ask this question 2weeks ago, but haven't receive any response.

Thank you.


Code:
<html>
<head>
<title>SHUBooks</title>
</head>
<body>

<?php
session_start();
include('connect.php');
$query = "SELECT * FROM bookcopy";
$result = mysql_query($query) or die(mysql_error());
?>

<div align="center"><b>Thank you for visiting SHUBooks! Please see our list of available Books below, and click to view Book
details:
</b><br><br>
<table border="1">
<?php
echo "<tr>";
echo "<td bgcolor=\"red\" width=\"30\">";
echo "<font color=\"white\"><b>";
echo "Back Cover";
echo "<font></b></td>";
echo "<td bgcolor=\"red\" with=\"50\">";
echo "<font color=\"white\"><b>";
echo "Book Title";
echo "<font></b></td>";
echo "<td bgcolor=\"red\" width=\"20\"><td>";
echo "</tr>";

//display books with prices

while($row = mysql_fetch_array($result))
{
//extract($row);
echo "<tr>";
echo "<a href=\"detail.php?bookId=".$row['bookId']."\">";
echo "<td><em>BOOK IMAGE</em></td>";
echo "<td>";
echo "<a href=\"detail.php?bookId=".$row['bookId']."\">";
echo $row['bookTitle'];
echo "</td>";
echo "<td>";
echo "<a href=\"detail.php?bookId=".$row['bookId']."\">";
echo "View Details</a>";
echo "</td>";
echo "</tr>";

}

?>
</table>
<?php
echo "<hr size=\"1\" color=\"red\" NOSHADE>";
echo "<form action=\"cart.php\">";
echo "<input type=\"submit\" value=\"My Shopping Cart >>\">";
 echo "<input type=\"hidden\" name=\"bookId\" value=".$row['bookId'].">";
echo "</form>";
?>

</body>
</html>
 
I need to see your code for detail.php.

Your form doesn't do anything at the bottom except to push one single (the last one the db feeds) to cart.php. So that's a fubar situation there.
 
Below is the code for detail.php page,

Thank you.

Code:
<?php
if(!session_id()){
session_start();
include('connect.php');
//$bookId=$_REQUEST['bookId'];

}
?>
<html>
<head>
<title><?php echo $bookTitle ?></title>
</head>
<body>
<?php
//get variable paased through the URL



//get info about the book we want
$query = "SELECT * FROM bookcopy";

$results =mysql_query($query) or die(mysql_error());
$row= mysql_fetch_array($results);

?>

<div align="center" >
<table cellpadding="5" width="80%">
<tr>
<td>BOOK IMAGE</td>
<td><strong><?php echo $row['bookTitle']; ?></strong><br>
<?php echo $row['ISBN']; ?><br>
<br>Book Price: £<?php echo $row['bookPrice']; ?><br>

<form method="POST" action="modcart.php">
<input type="hidden" name="bookId" value="<?php echo $row['bookId'] ?>">
<input type="hidden" name="userId" value="<?php echo $row['userId'] ?>">
<input type="hidden" name="bookTitle" value="<?php echo $row['bookTitle'] ?>">
<input type="hidden" name="bookPrice" value="<?php echo $row['bookPrice'] ?>">   
<input type="submit" name="Submit" value="Add to My Cart">  
</form>

<form method="POST" action="cart.php">
<input type="submit" name="Submit" value="View My Cart">
<input type="hidden" name="cartId" value="<?php echo $row['cartId'] ?>">
<input type="hidden" name="bookId" value="<?php echo $row['bookId'] ?>">
</form>
</td>
</tr>
</table>
<hr width="450" color="green">
<p><a href="books.php">Go Back</a></p>
</div>
</body>
</html>
 
Your code doesn't do anything. and I mean, anything.

You're going to need to grab the ID of the book that was clicked..
Pull that information from the DB, and then display it.
 
Creating database and database tables in PHP MYADMIN is very easy and faster than doing in mysql console and sqlite...To retrieve data from database follow the coding:
<?php
$con=mysql_connect("hostname","username","password");
mysql_select_db("databasename",$con);
//create table
$t="create table hi();"
mysql_query($t,$con);
//insert values into table
$i="insert into hi values();"
mysql_query($i,$con);
//write a query
$sql="select * from hi";
$result=mysql_query($sql,$con);
while($re=mysql_fetch_array($result))
{
echo $re[''];//output the field names
}
?>
Cegonsoft Interview Orientation

Cegonsoft
 
Back
Top