/    Sign up×
Community /Pin to ProfileBookmark

PHP with MySQL queries

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”;
}
[/CODE]

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”;
[/CODE]

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.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @commodorejim76 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...