Page 1 of 1

Need to display one item

Posted: Wed Apr 29, 2009 12:45 pm
by johnmca
I'm currently using a database of books where my PHP page will list all the books after each other. The code I'm going to use for this is:

Code: Select all

 
<?php
$con=mysql_connect("localhost","username","password);
 
if(!$con)
    {
     die("Cound not connect: ".mysql_error());
    }
 
mysql_select_db("my_db",$con);
 
$sql = "SELECT * FROM Books";
$result = mysql_query($sql);
 
while($row=mysql_fetcg_array($result)
{
     echo $row['ID']."<br>";
     echo $row['Title']."<br>";
     echo $row['Author']."<br>";
     echo "<img src='."$row['Image']"."'><br>";
     echo $row['Stock']."<br>";
}
mysql_close($con);
?>
 
I also want to create a HTML form where a user can select a specific book to display.
Any ideas how to implement this?
I'm guessing i'll use POST method for the form but I'm not sure how I can dispaly the book so a user can pick just one.

Any guidance appreciated.

Re: Need to display one item

Posted: Wed Apr 29, 2009 4:21 pm
by Yossarian
You'd likely use a link on a page:

<a href=findbook.php?id=7>Book 7</a>

sending the user to this page;

findbook.php

Code: Select all

 
<?php
$book_id=(int)$_GET['id'] ;
 
 
.....
 
$sql = "select from books where id = $book_id"
 
....
display single result
 
the first page you would generate by making links like this:

// derived from the code you posted;

foreach of your results:

echo '<a href=findbook.php?id=' . $row['id'] '>' . $row['title'] . '</a>';