Forum Moderators: coopster

Message Too Old, No Replies

multiple queries - using search options

how to do search; drop-down, checkbox....

         

omfLondon

7:08 pm on Mar 28, 2007 (gmt 0)

10+ Year Member



HELP! I am still going round in circles further to earlier query!

I have the basic html file which has the details I want to search the database (createdb1) with tables for artist, cd and type (for type of music-checkbox)

I've started the php file but as I can't figure it out and have gone down endless dead-ends; I'm desperate to be pointed in the right direction! I've been trying for days on end but can't see the wood for the trees...! any help gratefully received!

I'm not sure how to set up the queries for the database, how to pull queries from the tables within the database, how to set up queries for the prices and year of release..... sorry; I don't know a lot about it :(

THE PHP BIT:

MY CONNECTION TO CD DATABASE (createdb1:
<?
<?php // Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("createdb1") or die(mysql_error());
?>
<?
//error message (not found message)begins
$XX = "No Record Found, to search again please close this window";
//query details table begins
$query = mysql_query("SELECT * FROM

//i want customer to be able to search by a) type of music and/or b) artist and/or c) year of release and/or d)price - & show results in table

?>

THE HTML FORM:
<html> searchCD.htm

<form action="checkresult.php" method="post">
<p>Choose CD from:</p>
<p>TYPE of Music</p>
<p>C&amp;W
<input type="checkbox" value="1" name="type[]" />
R&amp;B
<input type="checkbox" value="2" name="type[]" />
pop
<input type="checkbox" value="3" name="type[]" />
blues
<input type="checkbox" value="4" name="type[]" />
</p>
<p>AND/OR</p>
<p>ARTIST
<select name="select" size="1">
<option>Bush</option>
<option>billie holiday</option>
<option>abba</option>
<option>dolly parton</option>
</select>
</p>
<p>AND/OR</p>
<p>Year of Release 1990 </p>
<p>AND/OR</p>
<p>Price:
<input type="text" name="textfield">
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>

eelixduppy

1:59 am on Mar 29, 2007 (gmt 0)



Hello omfLondon!

We have some threads in our library that will get you started with this, and then we can fine-tune it once you understand the basics. Here's a good start: The Basics of Extracting Data from MySQL [webmasterworld.com] and Developing a MySQL Search Query [webmasterworld.com]. The latter may be of interest to you :)

Hope this gets you on the right path!

Best of luck!

omfLondon

6:25 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Thanks! I think I'm beginning to see the light....!

omfLondon

10:55 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Gawd, I'm going mental here! I can't seem to get the hang of checkboxes - i'm ok doing the select statements and getting queries from the dbase but when I try to set up a form to do queries it all unravels! Can anyone have a look to see where/what the prob. is in the coding.... many thanks in advance...

The form: (to select year of cd release)

<form action="TCTest4.php" method="post">
1999<input type="checkbox" value="1999" name="releaseyear[]" />
2005<input type="checkbox" value="2005" name="releaseyear[]" />
2003<input type="checkbox" value="2003" name="releaseyear[]" />
<input type="submit" />
</form>

{i have also tried using drop downs to try to extract info + same result = zero!)

<form method = "post" action = "TCTest2.php">
<p>Select a field to display:
<select name = "select">
<option selected = "selected">*</option>
<option>title</option>
<option>releaseyear</option>
<option>price</option>
<option>itemsinstock</option>
</select>
</p>

PHP bit:

<?php
$link = mysql_connect("localhost","","") or die("Unable to connect to SQL server");

mysql_select_db("CDdb1", $link) or die("Unable to select database");

$selectSql = "SELECT * FROM music WHERE $releaseyear = $_POST['releaseyear']";
$result = mysql_query($selectSql, $link);

echo '<pre>';
print_r($releaseyear);
echo '</pre>';
#
?>

Where I'm having the most struggles are with using variables/names for checkboxes - i.e. should I assign $releaseyear or should it be something like $checkbox - totally baffled!

eelixduppy

10:58 pm on Apr 2, 2007 (gmt 0)



I have one question before I can answer you. Do you want ONLY ONE year to be able to be selected, or are you looking for ONE OR MORE to be selected and return all that apply? This will change the answer to your question :)

omfLondon

11:01 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Actually; what I'm really trying to do is get the details from music table - e.g. price, title etc etc - by using the checkbox to search for these details using a specific year.... maybe I have the Select part wrong.... yikes... more circles...! sorry; I really am lost. thanks in advance..

OM

omfLondon

11:12 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



For this part of the form; I only want them to choose 1 year.

Later on I (hopefully) have another form on the page that will let them choose more than 1 - it is a form where they can choose an artist - but they can select more than one artist (e.g. where 2 or more have worked together on an album).
thanks

eelixduppy

11:14 pm on Apr 2, 2007 (gmt 0)



>> using a specific year

Ok, so then you don't want to base the search on more than one year ;)

Let's see. Instead of checkboxes, let's try radios:


<form action="TCTest4.php" method="post">
1999<input type="radio" value="1999" name="releaseyear" /><br/>
2005<input type="radio" value="2005" name="releaseyear" /><br/>
2003<input type="radio" value="2003" name="releaseyear" /><br/>
<input type="submit" />
</form>

And here's the PHP:


<?php
$link = mysql_connect("localhost","username","password");
mysql_select_db("CDdb1", $link) or die("Unable to select database");
#
$selectSql = "SELECT * FROM music WHERE $releaseyear = '".[url=http://www.php.net/mysql-real-escape-string]mysql_real_escape_string[/url]($_POST['releaseyear'])."'";
$result = mysql_query($selectSql, $link) or [url=http://www.php.net/die]die[/url]([url=http://www.php.net/mysql-error]mysql_error[/url]());
while($row = [url=http://www.php.net/mysql-fetch-assoc]mysql_fetch_assoc[/url]($result)) {
echo '<pre>';
print_r($row);
echo '</pre>';
}
[url=http://www.php.net/mysql-close]mysql_close[/url]($link);
?>

Try this to see what you get :)

omfLondon

11:22 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Wow; great - looks like its about to run then I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '2003'' at line 1

?

eelixduppy

11:24 pm on Apr 2, 2007 (gmt 0)



hehe...looks like I made a booboo ;)

Change this:


$selectSql = "SELECT * FROM music WHERE $releaseyear = '".mysql_real_escape_string($_POST['releaseyear'])."'";

To this:


$selectSql = "SELECT * FROM music WHERE releaseyear = '".mysql_real_escape_string($_POST['releaseyear'])."'";

omfLondon

11:35 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



EXCELLENTTTTTTTTTTTTTTTTT :) You are the best! I have spent sooo long trying to get that to work!

Are you ready for more of my queries? ;) ?

Just in case you're bored; ehem!

I have 2 more things I am trying to do on the page. 1) was my attempt at doing the checkbox to select the artist - which was going horribly wrong - it was my original version for the year listing - again got lost with assigning variables/names.

the second & final part is to let the user choose a cd within a price range. I was just going to give them 3 options (maybe I've just answered my own question here; if I use your last suggestion) with 3 ranges; 1 - 4.99, 5.00 - 9.99 etc etc.....

What I am hoping to achieve with the page is let the user see the list of cds available (I'm not php savvy enough to do a search box) and from the listing choose; a) the year of release b) choose artist c) choose price range.

My final query (!) is can I just use 1 form to attach all these selections or whats the best way to whittle down the queries?

Sorry again; but I have spent weeks trying to figure this out & getting frown lines & grey hair during this process! Help me keep my looks & sanity. :)

omfLondon

11:56 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Hi again; I've just been re-doing some of my previous codes and realise that the reason I hadn't got too far with the music type was because this is in a different table to music. it is in a table called type and which is linked to music via the typeid PK. So thats why a lot of confusion arose. :)

eelixduppy

7:05 pm on Apr 4, 2007 (gmt 0)



How are you making out with this?

I'd like you to try this yourself before someone here does it for you; this is the only way you are going to learn ;) When you get something to work with, start a new thread and we can go from there. Refer to the links in my first post [webmasterworld.com] and use them as a guideline. Also, if you need query help [mysql.com...] or the Database Forum [webmasterworld.com] are good places to look for answers.

Good luck!

omfLondon

11:16 am on Apr 5, 2007 (gmt 0)

10+ Year Member



Hiya!

Thanks & take your point about putting in the work ;)

At the moment; I am re-jigging the code; so that I have 3 drop down lists + checkbox. I am getting there sloooowly but hopefully when/if (!) I get stuck I will get back here with the code & whr the problems are - this forum is brilliant; so far I am moving a lot quicker than working through the textbooks

Many thanks!

omfLondon

11:15 pm on Apr 20, 2007 (gmt 0)

10+ Year Member



Hi there! I've finally made some progress & have dropdowns and a checkbox; however I have 2 probs which I can't figure out: grateful for any further help. THANKS!

1. Not sure how to code it so that user can query from just 1 or all of the dropdowns (at the moment unless they select the correct answers from each dropdown - no results!) = defeats the whole purpose of searching.

2. Also, not sure how to code it so that checkbox option can also be used to search the catalogue.

[idea of page is that user can search catalogue by selecting year of release, artist name, music type & resulsts can be returned)

THE FORM:

<?php
$link = mysql_connect("localhost","","");
mysql_select_db("createdb1", $link) or die("Unable to select database");
#
$selectSql1 = "SELECT * FROM music WHERE releaseyear = '".mysql_real_escape_string($_POST['releaseyear'])."'
AND title = '".mysql_real_escape_string($_POST['title'])."'";

$selectSql2 = "SELECT * FROM type WHERE type='" . $_POST['type'] . "'";

#
echo $selectSql1;
$result1 = mysql_query($selectSql1, $link) or die(mysql_error());
while($row1 = mysql_fetch_assoc($result1)) {

echo $selectSql2;
$result2 = mysql_query($selectSql2, $link) or die(mysql_error());

while ($row2 = mysql_fetch_array($result2))
{

echo '<pre>';
print_r($row);
echo '</pre>';
}

mysql_close($link);
?>

HTML:

<form action="TCTest8.php" method="post">
<p>&nbsp;</p>
<p> Title:
<select name="title">
<option>-</option>
<option>Devil Drive</option>
<option>Amarillo</option>
<option>The The</option>
</select>
<br/>
</p>
<p>Year of Release
<select name="releaseyear">
<option>Choose Year</option>
<option>1999</option>
<option>2000</option>
<option>2005</option>
<option>2006</option>
</select>
</p>
<p>Actor/Artist Name:
<select name="name" size="3" multiple>
<option>Choose</option>
<option>A</option>
<option>B</option>
<option>E</option>
</select>
</p>
<p> type </p>
<input type="checkbox" value="R&B" name="type[]" />
<input type="checkbox" value="blues" name="type[]" />
<input type="checkbox" value="pop" name="type[]" />
</select>
<p><br/>
<input type="submit" />
</p>

</form>