php - Remove book from search results if it has already been purchased
I am trying to check the books_ordered table to get a list of book ISBN's that have been ordered. If an ISBN exists in teh books_ordered table I do not want it to show in the list of search results. I have the following code:
include "include.php";
session_start();
$query1 = 'SELECT isbn as myisbn FROM books_ordered';
$result1 = mysqli_query($con, $query1) or die("Error in query $query1: " . mysqli_error());
while($row = mysqli_fetch_array($result1)){
$isbn=$row['myisbn'];
}
$query = 'SELECT * FROM book';
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());
while ($row = mysqli_fetch_array($result)){
if($isbn!=$row[0]){
echo $row[1]."<br>";
}
}
mysqli_close($con); //closes the connection
?>
The script I have only hides the first isbn from the books_ordered table in the search results, does anyone have any ideas why? I'm guessing it's something to do with the brackets in the while loops, i just can't figure it out. Or if anyone knows a better way of doing this it would be much appreciated. Thanks.
Answer
Solution:
It would be more efficient to filter out the unwanted results from the book table in a single query, like so:
Answer
Solution:
You are only storing the last value in $isbn since it is not an array. You need to add the isbn into an array, and then check each row in the second query against that array.
But I would probably do the processing in the SQL statement instead. Something like:
Answer
Solution:
What is your database structure for "book"? You can easily make a "JOIN"-Command.
Answer
Solution:
If I have understood well, you want to show only the books are not ordered yet.
You could use this SQL sentence:
First we select the isbn of the ordered books with:
SELECT isbn FROM books_ordered
When we have the list, using the clause NOT IN we can select from BOOK all books except all that we get the isbn in the first sentence.I hope is useful for you.