Hello everyone, I’ve been banging my head against the wall with this one for a couple of days and would really appreciate some help.
I’m coding a simple webpage with a database containing a collection of books from the Fighting Fantasy series. I’ve included a form where I can search by author, cover artist, interior artist and year of publication. The author, cover and interior artists are selected from a menu using <SELECT><OPTION> tags and the year is just typed into a text field.
The php script that’s run when the form is submitted contains the following code:
[CODE]
$query = “SELECT * FROM books WHERE book_no IS NOT NULL”;
if(isset($_POST[‘author’])){
$author=$_POST[‘author’];
$query .= ” AND ‘$author’ = author”;
}
and this works fine, i.e. when I select an author from the dropdown list, the database returns just books from that author. However, I’d like to be able to search using a combination of criteria, e.g. author and cover artist or interior artist and year etc. But when I add the following code:
[CODE]
$query = “SELECT * FROM books WHERE book_no IS NOT NULL”;
if(isset($_POST[‘author’])){
$author=$_POST[‘author’];
$query .= ” AND ‘$author’ = author”;
}
else if(isset($_POST[‘author’], $_POST[‘cover_art’])){
$author=$_POST[‘author’];
$cover_art = $_POST[‘cover_art’];
$query .= ” AND ‘$author’ = author AND $cover_art = cover_art”;
I can’t get the list of books by a selected author and selected cover artist returned. It just returns all books by the selected author, regardless of who the cover artist is. The else-if statement isn’t being evaluated (I’m guessing).
Can anyone spot where I’m going wrong? I know I’ll feel like an enormous ass when it’s pointed out but I honestly cannot see what I’m missing.