Undefined variable and use of MYSQL


#1

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?


#2

You’d need to add the code for login.php in order for us to answer. You probably have a $_POST[usr_id] in there which is missing quotes.

No need to hope for something to do what you want, debug it. This can easily be done by var_dumping the response from the query you’re performing to see if you indeed get a book name. For more advanced debugging, look into xdebug


#3

The code I posted was from login.php, but I’ll now post the full code.

[php]

<?php include('conn.php'); session_start(); ?> Scan bøger
<form method="post" action=""> 
	Barcode:<input type="text" name="barcode"/></br>
			 <input type="submit" name="submit" value="Scan"/>
</form>

<?php 

$user_id=$_POST['usr_id'];

$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]


#4

I now got $bookname to work. But I’m still getting the error: Notice: “Undefined variable: user_id in C:\xampp\htdocs\php-login\login.php on line 24”. When I use $_POST[‘usr_id’]; I’m referring to the usr_id column in the database. I don’t think that I can do it that way. Basically I just want a variable to store the user id which is based off of the usr_id column from the database. Here is my updated code:

[php]

<?php include('conn.php'); session_start(); ?> Scan books
<form method="post" action=""> 
	Barcode:<input type="text" name="barcode"/></br>
			 <input type="submit" name="submit" value="Scan"/>
</form>

<?php 


$result=mysqli_query($con,"SELECT * FROM account WHERE usr_id='$user_id'")or die (mysqli_error());;

$data=mysqli_fetch_assoc($result);

$user_id=$_POST['usr_id'];	







	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 'No book with the barcode '. $barcode. " exists!";
	 }
	 
	 {
	 }
	 
	 if (mysqli_num_rows($result) != 1)
	{
		echo 'No book with the barcode  '. $barcode. " exists!";
	}
	 
	 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());
				$bookname=$data['bookname'];
				echo 'The book' . $bookname. " has beenscanned!";
				
	 }		
	
	}

?>

[/php]


#5

Before you had an undefined constant, now you have a undefined variable, those are two different things

You get an undefined variable error (and a bad query) as you use the $usr_id variable in the query string, before you actually set the variable two lines further down


#6

You’re right I moved it above the query now:

[php]

$user_id=$_POST[‘usr_id’];

$result=mysqli_query($con,"SELECT * FROM account WHERE usr_id='$user_id'")or die (mysqli_error());;

[/php]

I still get an undefined index of usr_id on the line where my code says:
$user_id=$_POST[‘usr_id’];


#7

You dont have a usr_id field in the form


#8

[php]
Barcode:

[/php]

Your form. Where do you see name=“usr_id” ?