I have a mock library website for practice with a page displaying books. If a member is logged in another column adds a button to borrow the book. I want the button to be disabled/greyed if the book is already borrowed.
The ‘loan’ table includes the loanID, bookID, and memberID etc, and I want to make a statement that if the row bookID (current row in table) is present in loan table (e.g. bookID 4 is also in the loan table) then the borrow button is disabled. I have been stuck on this for a while now and I really would like some help, sorry for confusing wording.
CODE:
$sql = “SELECT bookID, title, genre FROM book”;
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table class='table table-hover'>"; //open the table tag to display the information
echo "<thead>";
echo "<tr>
<th>Book ID</th>
<th>Title</th>
<th>Genre</th>";
//If a member is logged in, display another table heading to borrow books
if(isset($_SESSION['memberEmail']))
{
echo "<th>
Borrow
</th>";
}
//If an admin is logged in, display another table heading to delete books
if(isset($_SESSION['admin']))
{
echo "<th>
Delete
</th>";
}
"</tr>";
echo "</thead>";
while($row = $result->fetch_assoc())
{
//Display all books and their selected information
echo "<tr>
<td><a href='bookDetail.php?bookID=" . $row['bookID'] . /* Create a link to take the user to a book detail page when they click on a chosen bookID */ "'>" . $row['bookID'] . "</a></td>
<td>" . $row['title'] . "</td>
<td>" . $row['genre'] . "</td>
<td>";
//If an admin is logged in, display a 'trash' icon that sumbits a form to delete the chosen book
if(isset($_SESSION['admin']))
{
echo "<form action='deleteBook.php' method='POST'>
<input type='hidden' value='" . $row['bookID'] . "' name='bookID' id='bookID'>
<button type='submit' class='btn text-danger btn-sm'><i class='material-icons'>delete</i></button>
</form>";
}
//If a member is logged in, display a 'bookmark' icon that sumbits a form to borrow the chosen book
if(isset($_SESSION['memberEmail']))
{
echo "<form action='borrow.php' method='POST'>
<input type='hidden' value='" . $row['bookID'] . "' name='bookID' id='bookID'>
<button type='submit' class='btn text-warning btn-sm'><i class='material-icons'>bookmark_add</i></button>
</form>";
}
echo "</td>
</tr>";
}
echo "</table>"; //close the table tag that displayed the information
} else
{
echo "0 results";
}
I just can’t get it to work, I have made it so when the button is pressed a row is created in the loan table, but the button is still active.
Anetha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.