Skip to content

Loading table values into a poplist, current value displaying correctly

I have two tables in my library database, Books and BookTypes:

CREATE TABLE `books` (
  `BookID` int(11) NOT NULL,
  `ISBN` varchar(20) NOT NULL,
  `Title` varchar(50) NOT NULL,
  `Author` varchar(50) NOT NULL,
  `BookType` char(1) DEFAULT NULL,
  `Price` decimal(5,2) DEFAULT NULL
CREATE TABLE `booktypes` (
  `BooktypeID` varchar(3) NOT NULL,
  `BookType` varchar(50) NOT NULL

When I load the Edit screen from my search results page, I have got the three types displaying.

But can’t get the currently selected value of the book to display(It’s always Digital).

My Edit Page Code is:

 <h1>Edit Book Information</h1>
         if (empty($_GET['BookID']))
            die("You need to select a Book from the form");
         $BookID = $_GET['BookID'];
         //open the server connection
         require 'dbConnectLibrary.php'; 
         //get the record
         $sql = "SELECT * FROM books WHERE BookID = $BookID";
         $result = mysqli_query($conn, $sql) or die("Error editing - ". mysqli_error($conn)); 
         if (mysqli_affected_rows($conn) == 0)
            die("Error – record not found to edit");
         while ($row = mysqli_fetch_array($result))
             $BookID = $row[0];
             $ISBN = $row[1];
             $Title = $row[2];
             $Author = $row[3];
             $BookType = $row[4];
             $Price = $row[5];
         echo "<form action=update-book.php method=GET>";
         echo "<input type=hidden name=BookID value=$BookID>"; 
         echo "<table border=1>";
         echo "<tr><td>ISBN:</td><td><input type=text id=ISBN name=ISBN value="$ISBN"></td></tr>";
         echo "<tr><td>Title:</td><td><input type=text id=Title name=Title value="$Title"></td></tr>";
         echo "<tr><td>Author:</td><td><input type=text id=Author name=Author value="$Author"></td></tr>";
         echo "<tr><td>Book type:</td><td><select value="$BookType">";
                $sql = "SELECT BookType FROM BookTypes";                
                $result = mysqli_query($conn, $sql) or die("Error reading booktypes - ".mysqli_error($conn)); 
                while ($row = mysqli_fetch_array($result))
                            if($BookType == $row[BookType])
                                echo "<option value"$row[BookType]" selected>$row[BookType]</option>";
                                echo "<option value"$row[BookType]" >$row[BookType]</option>";
         echo "<tr><td>Price:</td><td><input type=text id=Price name=Price value="$Price"></td></tr>";
         $sql = "SELECT Title, BookID, Price FROM books ORDER BY Title";
         //echo "$sql";
         $result = mysqli_query($conn, $sql) or die("Error reading books - ".mysqli_error($conn));
         echo "</table>";
         echo "<br><input type=submit value=update>";
 <a href="david.php">Cancel</a>

How do I ensure the current value of the selected book’s booktype is selected on the poplist when I load the edit page?



You need to do something like this

<option <?php if($booktype['id']==$book['id']){ ?> selected="selected" <?php }?> value='<?php echo $booktype['id'] ?>' >
        <?php echo $booktype['booktype_name'] ?>