Page 1 of 1

Please help! Creating MySQL query w/php checkbox.

Posted: Tue Aug 19, 2003 7:22 pm
by Dr. Neko
:( I really need help on creating a dynamic SQL query using PHP (I've been asking everywhere and no one has helped me). The HTML source codes for the HTML file is in the bottom of this message.
What I want is the visitor to make some selections on either the simple or advance search options via checkbox. IE:

FROM
Select
either Daily Comics or Birthday Comics (each of these selections is the name of their table)

CHARACTERS
Garfield, Odie, John
(each of these is the name of their column in the tables above)

Basically, I created some tables. By default, when a CHARACTER isn't selected, it's value is set to "0" (false); it's value is set to "1" (true) when selected. Here's the table schema and sample value:

CREATE TABLE Daily
( Ino char(2) not null primary key,
URL char(15),
Tn char(17),
Garfield tinyint(2),
John tinyint(2),
Odie tinyint(2));



INSERT
INTO Daily(Ino, URL, Tn, Garfield, John, Odie)
VALUES ('1','"d/Daily01.gif"','"d/Daily01tn.gif"','1','0','0');
//the rest of the data is in the zip file if your interested in the whole source codes at the bottom of the message body

When the visitor makes a selection, he/she is actually customizing the query. Here's a sample of what the visitor will select and a sample of how the query should look like.

FROM
Select Daily Comics
(check) Birthday Comics (unchecked)

CHARACTERS
Garfield
(check) Odie (uncheck) John (check)

Code: Select all

$SQL="SELECT * FROM Daily where Odie=0 ORDER BY Ino";//want to achive this since the query is shorter
or

Code: Select all

$SQL="SELECT * FROM Daily where Odie=0 OR Garfield=1 OR John=1 ORDER BY Ino";//last resort since this vesion of the query is longer
When I modify the $SQL variables in the inc file and ran the page , it works fine with that query.

The advance search, I've included a section:
SEARCH FOR

Any (this will be the "AND" variable)
Only (this will be the "OR" variable)

That's basically it. I just need to know how to make the $SQL variable dynamic with the selections above via checkbox selections. Finally, I was able to figure out how to prevent a search with none of the required selection(s) is/are selected, but is there a way to prevent illogical selections in the advance search, such as:
INCLUDES
Garfield (check)


NOT INCLUDING
Garfield (check)


If you can figure anything out, please let me know. Thank you. :)

-Daniel Orellana

Code: Select all

HTML Source Code:
<HTML>
<TITLE>Garfield Search</TITLE>
<body bgcolor="#000000" text="#00CCFF" link="#FFFFFF" vlink="#660099" alink="#00FF00">
<FONT SIZE="4"><B><FONT SIZE="5">Simple Search</FONT></B></FONT> 
<FORM action="Search.php" method=post>
  <FONT SIZE="4"><B>From</B></FONT> 
  <p>
    <INPUT TYPE="checkbox" NAME="Daily" VALUE="Daily">Daily Comics 
    <INPUT TYPE="checkbox" NAME="BDay" VALUE="BDay">Birthday Comics 
  <p>
	<B><FONT SIZE="4">Characters</FONT></B> 
  <p> 
    <INPUT TYPE="checkbox" NAME="Garfield" VALUE="1">Garfield 
    <INPUT TYPE="checkbox" NAME="Odie" VALUE="1">Odie 
    <INPUT TYPE="checkbox" NAME="John" VALUE="1">John
  <p>
    <INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
    <INPUT TYPE="Reset" NAME="Reset" VALUE="Reset">
</FORM>
<p>
<B><FONT SIZE="5">Advance Search</FONT></B> 
<FORM action="Search.php" method=post>
  <FONT SIZE="4"><B>Search for</B></FONT> 
  <p>
    <INPUT TYPE="radio" NAME="Any" VALUE="or" CHECKED>Any 
    <INPUT TYPE="radio" NAME="Only" VALUE="and">Only
  <p>
  <FONT SIZE="4"><B>From</B></FONT> 
  <p>
    <INPUT TYPE="checkbox" NAME="Daily" VALUE="Daily">Daily Comics 
    <INPUT TYPE="checkbox" NAME="BDay" VALUE="BDay">Birthday Comics 
  <p>
	<B><FONT SIZE="4">Including Characters</FONT></B> 
  <p> 
    <INPUT TYPE="checkbox" NAME="Garfield" VALUE="1">Garfield 
    <INPUT TYPE="checkbox" NAME="Odie" VALUE="1">Odie 
    <INPUT TYPE="checkbox" NAME="John" VALUE="1">John
  <p>
	<B><FONT SIZE="4">Not including Characters</FONT></B> 
  <p> 
    <INPUT TYPE="checkbox" NAME="Garfield" VALUE="0">Garfield 
    <INPUT TYPE="checkbox" NAME="Odie" VALUE="0">Odie 
    <INPUT TYPE="checkbox" NAME="John" VALUE="0">John
  <p>
    <INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
    <INPUT TYPE="Reset" NAME="Reset" VALUE="Reset">
</FORM>
</BODY>
</HTML>
LINK TO SOURCE CODES & SAMPLE FILE:
"SorceCodes&Samples.zip"

Posted: Tue Aug 19, 2003 8:05 pm
by junrey
hello,

you got a lot of conflicting names in your code. please try to make your variables unique.

it would just be a matter of conditioning on what you wanted to happen. maybe not so many people try to reply your post, coz it may take some time.