Hey. I’m having trouble creating my PHP website which needs to function like a library.
Basically the website consists of two things. The first site is a registration page where a books name and it’s barcode can be entered in order to register it. The second page is where you type in the barcode to borrow the book from the library. When you type in the books barcode you will either get a reply saying that the book (insert name here) was successfully scanned or that the book with the barcode (insert barcode here) didn’t exist. (the book didn’t exist in the database).
Since each book has it’s own user id i thought that I could use the user_id to pull out information like the barcode and the name of the book I need to use this information to display the message.
Let me give you an example. I register a book called Hello World and I give it the barcode 123
If a user then wants to borrow the book he types the barcode (123) in the textbox and he should get the response:
The book Hello World has been scanned.
If the book doesn’t exist in the database it shoud reply:
There is no book registered with the barcode 123.
I just can’t for the life of me figure out how to achieve this. This is my code:
[php]
$result=mysqli_query($con,“SELECT * FROM account WHERE usr_id=’$user_id’”);
$data=mysqli_fetch_assoc($result);
$bookname=$data['bookname'];
if (isset($_POST['submit'])) {
$barcode=$_POST['barcode'];
$result=mysqli_query($con,"SELECT * FROM account WHERE barcode='$barcode'")or die (mysqli_error());
if(!$result){
header('location:index.php');
echo 'There is no book registered with the barcode '. $barcode;
}
{
}
if (mysqli_num_rows($result) != 1)
{
echo 'There is no book registered with the barcode '. $barcode;
}
else{
$data=mysqli_fetch_assoc($result);
$_SESSION['usr_id']=$data['usr_id'];
mysqli_query($con,"INSERT INTO account (usr_scan) VALUES ('$barcode')")or die (mysqli_error());
echo 'The book' . $bookname. " has been scanned!";
}
}
[/php]
Firstly i get the error:
Warning: Use of undefined constant usr_id - assumed ‘usr_id’ (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\php-login\login.php on line 22
Secondly when i type in a book which is registered it just says: “The book has been scanned!” It doesn’t mention the bookname which means that $bookname isn’t doing what I hoped it would do.
How can I fix the code so the error doesn’t show up and so the webppage works as intended when a user enters the barcode to borrow a book?