Forum Moderators: coopster

Message Too Old, No Replies

Php Mysql Issue

writing mysql filter for multiple variables

         

calmchess

2:25 am on May 26, 2005 (gmt 0)

10+ Year Member



transaction=1&transaction=2&transaction=3&transaction=4

I need some help with writing a my sql filter statement for the above variables which are passed through the address bar as URL.

The following is the ideal 'syntax' for the sql statement.

WHERE `transaction` = colname

regards,
lost in var~land

calmchess

3:26 am on May 26, 2005 (gmt 0)

10+ Year Member



top

jatar_k

3:32 am on May 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, is that the same as <bump>? ;)

unnecessary, someone always wanders along. I don't completely follow but the variables and values passed in the url are accessible through the $_GET [ca.php.net] superglobal array (search for GET). You can access the vars passed to your page through that array.

calmchess

3:42 am on May 26, 2005 (gmt 0)

10+ Year Member



thnx i don't completely follow myself...i'm using dreamweaver if that tells you anything thnx for your help though the superglobals is a neat name and will probably fix my problems after i learn to use it within dreamweaver....gimme..a..month..

*smiles* Bumps aren't necessary*bows head**smiles*

regards,
lost in vars~land

jatar_k

3:55 am on May 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about this, this may help you demonstrate it for yourself

put this in the top of the file that is being accessed with that query string

echo $_GET['transaction'];

the thing is with that string you showed, all the variables have the same name, bit of a problem.

calmchess

4:08 am on May 26, 2005 (gmt 0)

10+ Year Member



yeah them haveing all the same name is a problem...but is necessary for the script i'm using....i used the $_GET but it had little effect.....if u want to check my pages out so u can see more what the look like go here.......
date: is the input of intrest.....05...03....2006

[edited by: jatar_k at 4:26 am (utc) on May 26, 2005]

jatar_k

4:27 am on May 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry no urls, here are the 3 forms

<form name="trans" id="trans" method="get" action="resultbtla.php" class="formbox1" >
Transaction#: <input name="transaction" type="text" id="transaction5" />
<input name="TransS" type="submit" id="TransS" value="Search" />
</form>

<form name="Year" id="Year" method="get" action="resultybtla.php" class="formbox2">
or Year: <input name="year" type="text" id="Year24" />
<input name="YearS" type="submit" id="YearS" value="Search" />
</form>

<form name="Date" id="Date" method="get" action="resultybtla.php"class="date" >
or Date:
<input name="month" type="text" id="Month4" value="mm" size="2" maxlength="2"/>
<input name="day" type="text" id="Day2" value="dd" size="2" maxlength="2" />
<input name="year" type="text" id="Year2" value="yyyy" size="4" maxlength="4" />
<input type="submit" name="Submit" value="Submit" />
</form>

so explain to me, not in code, what you would like to have happen when each of these are submitted. I see they are all submitted to the same script.

calmchess

4:44 am on May 26, 2005 (gmt 0)

10+ Year Member



The Ultimate goal is to display a recordset and then delete records based on whether or not a check mark is checked.


1. date:.....form submits to the resultbtlapage.php.

2. the resultbtlapage.php filters the record set.

3. the resultbtlapage.php displays the filtered records. And displays duplicate records via a repeat region.

4. the resultbtlapage.php inserts the ....transaction record into the transaction form.

5. resultbtlapage.php then submits the transaction form to the resultYbtla.php

6. resutYbtla.php is supposed to display all the records for trans action ....but the original problem of this post comes into play here.

I know alot of this isn't clear ....i'm not a programmer or analyst but i'm highly intrested

regards and thankyou,
lost in var~Land

calmchess

4:49 am on May 26, 2005 (gmt 0)

10+ Year Member



(refresh)

The ultimate goal is to display a recordset with similar values and then delete single/multiple records based on whether or not a checkbox is checked.

jatar_k

5:13 am on May 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



all pretty clear

well, all the links can easily go to the same script to process and display the desired results, makes it easier that way anyway.

alright first thing to know about programming BE LAZY, I think I stole that from Rasmus but I like it.

Make your life easy, no need to complicate things more than they need to be, or already are.

so, first thing, we look at the forms. I like my forms to mirror the tables they are pulling from. You are searching fr tansaciton #, year and year/month/day.

I would suggest each of these be it's own column, split the year into its own as well, if you want to only search on year it makes it a lot easier if it is all by itself.

so, that decided, let's figure out how we are going to figure out which type of search we need to do. Well an east way is to have a hidden value (love those) to tell us. Maybe something like

<input type="hidden" name="stype" value="year">

then change the value for each form, now our script can make some of its decisions based on that variables value. We could easily use a switch case here since there are more than 2 possible values.

switch [php.net] ($_POST['stype']) {
case "year":
echo 'I am doing a search on the year';
break;
case "fulldate":
echo 'I am doing a search on the year/month/day';
break;
case "transnum":
echo 'I am doing a search on the transaction number';
break;
}

this can also incorporate the "allrecords" scenario but since that is just a link that is another lesson. ;)

within each of those 'cases' we need to build our query based on the submitted values and fields.

so our base query could be something like

select * from ourtable where ....

now the where clause is what we need to create. Each where clause will be different for each submitted form so we build it inside each appropriate case. Let's look at the first one (keep in mind I have no idea what the column names or tablenames are)

where transnum=''

this will grab the entered transaction number and use it in the query. So let's grab that
$_POST['transnum'] is the var we need to get and then concatenate it into our query, we can use the same varname in every case so we can run a generic query function call later ;)

$sql = "select * from ourtable where transnum='" . $_POST['transnum'] . "'";

there we go, the second and third would be similar

$sql = "select * from ourtable where year='" . $_POST['year'] . "'";

$sql = "select * from ourtable where year='" . $_POST['year'] . "' and month='" . $_POST['year'] . "' and day='" . $_POST['day'] . "'";

easy stuff, now we need to add that to our switch and then do our query at the end. We will remove our preliminary echo's from the first example.

switch ($_POST['stype']) {
case "year":
$sql = "select * from ourtable where year='" . $_POST['year'] . "'";
break;
case "fulldate":
$sql = "select * from ourtable where year='" . $_POST['year'] . "' and month='" . $_POST['year'] . "' and day='" . $_POST['day'] . "'";
break;
case "transnum":
$sql = "select * from ourtable where transnum='" . $_POST['transnum'] . "'";
break;
}
$query = mysql_query($sql);

we can now use the variable query to grab all the rows using mysql_fetch_array, you can continue the story here

Basics of extracting data from MySQL using PHP [webmasterworld.com]
Help developing MySQL search query [webmasterworld.com]

that should get you going, and not using dreamweaver wouldn't hurt either ;)