Click here to Skip to main content
15,608,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to create a library management system wherein there is an allbooks table and a borrowedbooks table. When someone clicks borrow a book they have to enter the book ID. so when they do that I want the system to go into the all books table, check the number of books available for that book ID, -1 from the quantity and issue the book if there are any or to display a message like sorry no more books if the amount is 0. Im looking for some sort of coding structure to help me execute this in the system because I keep getting errors with my current code. I am very new to php.

What I have tried:

include("dbcon.php");

if(isset($_POST["submit3"]))
{
if


$sql = mysqli_query("SELECT bid, bstatus FROM Allbooks WHERE bid = '$_POST[bid]';");
if (!$sql) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value

if $row[0] > 0{

$uid = $_POST["uid"];
$bid = $_POST["bid"];
$bdate = $_POST["bdate"];

$query = "insert into Borrowedbooks(uid,bid,borroweddate) values('$uid','$bid','$bdate')"; //Insert query to add book details into the book_info table
$result = mysqli_query($conn,$query);


}
echo" Sorry there are no more copies";
Posted
Comments
Richard MacCutchan 24-Sep-21 3:15am    
You need to keep a count in the book records to show how many are available (in allbooks) and how many are borrowed (in borrowed books). As a book is borrowed or returned you need to update the relevant counts. But maybe a single table that keeps track of both counts would be simpler.
Idontknowmuch 24-Sep-21 3:39am    
The thing is the borrowed table only serves as a record that someone borrowed a book. It will only hold the users ID book ID and the borrowed date. Im trying to get the system to check the quantity of books under a certain book ID in the all books table and then decrement that value if it is above 0. and if it is above 0, to allow the user to take that book.
Richard MacCutchan 24-Sep-21 5:46am    
I think your database design is wrong. You need a books table, and a users table. Each time a user borrows a book you need to store the book id in the user's entry, and update the book record to show the remaining count. That way you can find all the books borrowed by any user.
Idontknowmuch 24-Sep-21 9:12am    
yes your one makes sense as well but i dont exactly want it to show up in the customer table. I jst want the function that checks the number of books under a bookID to be compared if its 0 or above and then decrement it by 1 if it is above and then issue it to the user if not then the user is declined. I jst want an idea on how to structure that part since I just started self-learning php
Richard MacCutchan 24-Sep-21 9:19am    
OK, but what you are asking about is database and SQL not PHP. If you just want to update the number then you have to do two steps:
Use a SELECT to get the book record.If the count is zero post a message.If the count is non-zero, subtract 1 and use an UPDATE statement to change the database.

You should also use Google to find some MySQL tutorials (not youtube videos).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900