Reply
Trouble Querying Database
Old 06-27-2008, 02:31 PM Trouble Querying Database
Average Talker

Posts: 19
Hello, I'm having some conceptual difficult with my PHP/SQL statements. I have a user fill out a form which then passes around 100 $_POST variables to the processing script. I know it passes the variables fine, that is not a problem. I then set each $_POST variable to a basic variable like so: $el_all = $_POST['el_all']. I create my sql statement like so:

Code:
$sql = "SELECT * FROM providers WHERE el_all = '$el_all' AND el_nonprofit = '$el_nonprofit' AND el_individual = '$el_individual' and so forth...
So I'm using the AND operator and no matter what selections I make it returns 0 results from the database. If I replace the AND operators with OR operators it returns EVERYTHING in the database. Where am I going wrong?

Conceptually I can see that in the sql statement I made above its returning no rows because although $el_all may have a value it says that the rest of the fields in that record must have blank values and since this is not the case for any records nothing is returned. So how do I prevent the SQL statement from using values that were not selected in the form? Or is there another approach I should be trying?

Thanks!
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline
Reply With Quote
View Public Profile Visit ffsja's homepage!
 
When You Register, These Ads Go Away!
Old 06-27-2008, 02:50 PM Re: Trouble Querying Database
Junior Talker

Posts: 4
Show listing of script. Perhaps you have an error when you are getting data from resource
xbit is offline
Reply With Quote
View Public Profile
 
Old 06-27-2008, 03:21 PM Re: Trouble Querying Database
Average Talker

Posts: 19
Ok, here's the processing script:

<?php

/*
Author: Sage Adams (Taiga Webmaster Services: http://taigawebmasters.com/)
Description: Processes public search
Created: 6/22/2008
*/

include("connection.php");

//rows to return
$limit = 10;

//set $_POST variables
$all_funders = $_POST['all_funders'];
$statewide = $_POST['statewide'];
$adams = $_POST['adams'];
$asotin = $_POST['asotin'];
$benton = $_POST['benton'];
$chelan = $_POST['chelan'];
$clallam = $_POST['clallam'];
$clark = $_POST['clark'];
$columbia = $_POST['columbia'];
$cowlitz = $_POST['cowlitz'];
$douglas = $_POST['douglas'];
$ferry = $_POST['ferry'];
$franklin = $_POST['franklin'];
$garfield = $_POST['garfield'];
$grantt = $_POST['grantt'];
$grays_harbor = $_POST['grays_harbor'];
$island = $_POST['island'];
$jefferson = $_POST['jefferson'];
$king = $_POST['king'];
$kitsap = $_POST['kitsap'];
$kittitas = $_POST['kittitas'];
$klickitat = $_POST['klickitat'];
$lewis = $_POST['lewis'];
$lincoln = $_POST['lincoln'];
$mason = $_POST['mason'];
$okanogan = $_POST['okanogan'];
$pacific = $_POST['pacific'];
$pend_oreille = $_POST['pend_oreille'];
$pierce = $_POST['pierce'];
$san_juan = $_POST['san_juan'];
$skagit = $_POST['skagit'];
$skamania = $_POST['skamania'];
$snohomish = $_POST['snohomish'];
$spokane = $_POST['spokane'];
$stevens = $_POST['stevens'];
$thurston = $_POST['thurston'];
$wahkiakum = $_POST['wahkiakum'];
$walla_walla = $_POST['walla_walla'];
$whatcom = $_POST['whatcom'];
$whitman = $_POST['whitman'];
$yakima = $_POST['yakima'];
$el_all = $_POST['el_all'];
$el_cities_towns = $_POST['el_cities_towns'];
$el_county = $_POST['el_county'];
$el_state = $_POST['el_state'];
$el_federal = $_POST['el_federal'];
$el_nat = $_POST['el_nat'];
$el_nonprofit = $_POST['el_nonprofit'];
$el_private = $_POST['el_private'];
$el_individual = $_POST['el_individual'];
$el_spd = $_POST['el_spd'];
$el_ed_inst = $_POST['el_ed_inst'];
$el_other = $_POST['el_other'];
$sc_all = $_POST['sc_all'];
$sc_grants = $_POST['sc_grants'];
$sc_loans_pri = $_POST['sc_loans_pri'];
$sc_gloans = $_POST['sc_gloans'];
$sc_ta = $_POST['sc_ta'];
$sc_other = $_POST['sc_other'];
$ced_all = $_POST['ced_all'];
$ced_disaster = $_POST['ced_disaster'];
$ced_bus_incubators = $_POST['ced_bus_incubators'];
$ced_historic_pres = $_POST['ced_historic_pres'];
$ced_housing = $_POST['ced_housing'];
$ced_industrial = $_POST['ced_industrial'];
$ced_nr = $_POST['ced_nr'];
$ced_pub_safety = $_POST['ced_pub_safety'];
$ced_research = $_POST['ced_research'];
$ced_rural_plan = $_POST['ced_rural_plan'];
$ced_sust_dev = $_POST['ced_sust_dev'];
$ced_urb_plan = $_POST['ced_urb_plan'];
$ced_other = $_POST['ced_other'];
$comm_all = $_POST['comm_all'];
$comm_dl = $_POST['comm_dl'];
$comm_telemed = $_POST['comm_telemed'];
$comm_broadband = $_POST['comm_broadband'];
$comm_phone = $_POST['comm_phone'];
$comm_pt = $_POST['comm_pt'];
$comm_telemetry = $_POST['comm_telemetry'];
$comm_other = $_POST['comm_other'];
$ed_all = $_POST['ed_all'];
$ed_childcare = $_POST['ed_childcare'];
$ed_k12 = $_POST['ed_k12'];
$ed_he = $_POST['ed_he'];
$ed_lib = $_POST['ed_lib'];
$ed_comm_ed = $_POST['ed_comm_ed'];
$ed_research = $_POST['ed_research'];
$ed_other = $_POST['ed_other'];
$ener_all = $_POST['ener_all'];
$ener_alt = $_POST['ener_alt'];
$ener_coal = $_POST['ener_coal'];
$ener_elec = $_POST['ener_elec'];
$ener_hydro = $_POST['ener_hydro'];
$ener_nuclear = $_POST['ener_nuclear'];
$ener_solar = $_POST['ener_solar'];
$ener_wind = $_POST['ener_wind'];
$ener_other = $_POST['ener_other'];
$ener_bio = $_POST['ener_bio'];
$env_all = $_POST['env_all'];
$env_ag = $_POST['env_ag'];
$env_brownfields = $_POST['env_brownfields'];
$env_ener_eff = $_POST['env_ener_eff'];
$env_forestry = $_POST['env_forest'];
$env_green_bldg = $_POST['env_green_bldg'];
$env_grazing = $_POST['env_grazing'];
$env_landscaping = $_POST['env_landscaping'];
$env_landuse = $_POST['env_landuse'];
$env_riparian = $_POST['env_riparian'];
$env_fish_wild = $_POST['env_fish_wild'];
$env_oil_rest = $_POST['env_oil_rest'];
$env_wetlands = $_POST['env_wetlands'];
$env_sp = $_POST['env_sp'];
$env_other = $_POST['env_other'];
$pf_all = $_POST['pf_all'];
$pf_art = $_POST['pf_art'];
$pf_comm_centers = $_POST['pf_comm_centers'];
$pf_ad_al = $_POST['pf_ad_al'];
$pf_fs = $_POST['pf_fs'];
$pf_ps = $_POST['pf_ps'];
$pf_pj = $_POST['pf_pj'];
$pf_pl = $_POST['pf_pl'];
$pf_health_fac = $_POST['pf_health_fac'];
$pf_mun_bldgs = $_POST['pf_mun_bldgs'];
$pf_museums = $_POST['pf_museums'];
$pf_other = $_POST['pf_other'];
$rec_all = $_POST['rec_all'];
$rec_greenbelt = $_POST['rec_greenbelt'];
$rec_outdoor = $_POST['rec_outdoor'];
$rec_parks = $_POST['rec_parks'];
$rec_sports = $_POST['rec_sports'];
$rec_trails = $_POST['rec_trails'];
$rec_other = $_POST['rec_other'];
$rec_play = $_POST['rec_playgrounds'];
$trans_all = $_POST['trans_all'];
$trans_air = $_POST['trans_air'];
$trans_bridges = $_POST['trans_bridges'];
$trans_marine = $_POST['trans_marine'];
$trans_offroad = $_POST['trans_offroad'];
$trans_pt = $_POST['trans_pt'];
$trans_ped_bike = $_POST['trans_ped_bike'];
$trans_parking = $_POST['trans_parking'];
$trans_pub_lighting = $_POST['trans_pub_lighting'];
$trans_streets = $_POST['trans_streets'];
$trans_rail = $_POST['trans_rail'];
$trans_other = $_POST['trans_other'];
$wm_all = $_POST['wm_all'];
$wm_hazard = $_POST['wm_hazard'];
$wm_recycling = $_POST['wm_recycling'];
$wm_solid_waste = $_POST['wm_solid_waste'];
$wm_waste_water = $_POST['wm_waste_water'];
$wm_other = $_POST['wm_other'];
$water_all = $_POST['water_all'];
$water_dams = $_POST['water_dams'];
$water_irr = $_POST['water_irr'];
$water_storm = $_POST['water_storm'];
$water_drinking = $_POST['water_drinking'];
$water_drought = $_POST['water_drought'];
$water_flood = $_POST['water_flood'];
$water_other = $_POST['water_other'];
$pp_all = $_POST['pp_all'];
$pp_planning = $_POST['pp_planning'];
$pp_feas = $_POST['pp_feas'];
$pp_design = $_POST['pp_design'];
$pp_construction = $_POST['pp_construction'];
$pp_other = $_POST['pp_other'];

//get records
if ($all_funders != "") {
$sql = "SELECT * FROM providers WHERE el_all = '$el_all' AND el_cities_towns = '$el_cities_towns' AND el_county = '$el_county' AND el_state = '$el_state' AND el_federal = '$el_federal' AND el_nat = '$el_nat' AND el_nonprofit = '$el_nonprofit' AND el_private = '$el_private' AND el_individual = '$el_individual' AND el_spd = '$el_spd' AND el_ed_inst = '$el_ed_inst' AND el_other = '$el_other' AND sc_all = '$sc_all' AND sc_grants = '$sc_grants' AND sc_loans_pri = '$sc_loans_pri' AND sc_gloans = '$sc_gloans' AND sc_ta = '$sc_ta' AND sc_other = '$sc_other' AND ced_all = '$ced_all' AND ced_disaster = '$ced_disaster' AND ced_bus_incubators = '$ced_bus_incubators' AND ced_historic_pres = '$ced_historic_pres' AND ced_housing = '$ced_housing' AND ced_industrial = '$ced_industrial' AND ced_nr = '$ced_nr' AND ced_pub_safety = '$ced_pub_safety' AND ced_research = '$ced_research' AND ced_rural_plan = '$ced_rural_plan' AND ced_sust_dev = '$ced_sust_dev' AND ced_urb_plan = '$ced_urb_plan' AND ced_other = '$ced_other' AND comm_all = '$comm_all' AND comm_dl = '$comm_dl' AND comm_telemed = '$comm_telemed' AND comm_broadband = '$comm_broadband' AND comm_phone = '$comm_phone' AND comm_pt = '$comm_pt' AND comm_telemetry = '$comm_telemetry' AND comm_other = '$comm_other' AND ed_all = '$ed_all' AND ed_childcare = '$ed_childcare' AND ed_k12 = '$ed_k12' AND ed_he = '$ed_he' AND ed_lib = '$ed_lib' AND ed_comm_ed = '$ed_comm_ed' AND ed_research = '$ed_research' AND ed_other = '$ed_other' AND ener_all = '$ener_all' AND ener_alt = '$ener_alt' AND ener_coal = '$ener_coal' AND ener_elec = '$ener_elec' AND ener_hydro = '$ener_hydro' AND ener_nuclear = '$ener_nuclear' AND ener_solar = '$ener_solar' AND ener_wind = '$ener_wind' AND ener_other = '$ener_other' AND ener_bio = '$ener_bio' AND env_all = '$env_all' AND env_ag = '$env_ag' AND env_brownfields = '$env_brownfields' AND env_ener_eff = '$env_ener_eff' AND env_forestry = '$env_forestry' AND env_green_bldg = '$env_green_bldg' AND env_grazing = '$env_grazing' AND env_landscaping = '$env_landscaping' AND env_landuse = '$env_landuse' AND env_riparian = '$env_riparian' AND env_fish_wild = '$env_fish_wild' AND env_oil_rest = '$env_oil_rest' AND env_wetlands = '$env_wetlands' AND env_sp = '$env_sp' AND env_other = '$env_other' AND pf_all = '$pf_all' AND pf_art = '$pf_art' AND pf_comm_centers = '$pf_comm_centers' AND pf_ad_al = '$pf_ad_al' AND pf_fs = '$pf_fs' AND pf_ps = '$pf_ps' AND pf_pj = '$pf_pj' AND pf_pl = '$pf_pl' AND pf_health_fac = '$pf_health_fac' AND pf_mun_bldgs = '$pf_mun_bldgs' AND pf_museums = '$pf_museums' AND pf_other = '$pf_other' AND rec_all = '$rec_all' AND rec_greenbelt = '$rec_greenbelt' AND rec_outdoor = '$rec_outdoor' AND rec_parks = '$rec_parks' AND rec_sports = '$rec_sports' AND rec_trails = '$rec_trails' AND rec_other = '$rec_other' AND rec_play = '$rec_play' AND trans_all = '$trans_all' AND trans_air = '$trans_air' AND trans_bridges = '$trans_bridges' AND trans_marine = '$trans_marine' AND trans_offroad = '$trans_offroad' AND trans_pt = '$trans_pt' AND trans_ped_bike = '$trans_ped_bike' AND trans_parking = '$trans_parking' AND trans_pub_lighting = '$trans_pub_lighting' AND trans_streets = '$trans_streets' AND trans_rail = '$trans_rail' AND trans_other = '$trans_other' AND wm_all = '$wm_all' AND wm_hazard = '$wm_hazard' AND wm_recycling = '$wm_recycling' AND wm_solid_waste = '$wm_solid_waste' AND wm_waste_water = '$wm_waste_water' AND wm_other = '$wm_other' AND water_all = '$water_all' AND water_dams = '$water_dams' AND water_irr = '$water_irr' AND water_storm = '$water_storm' AND water_drinking = '$water_drinking' AND water_drought = '$water_drought' AND water_flood = '$water_flood' AND water_other = '$water_other' AND pp_all = '$pp_all' AND pp_planning = '$pp_planning' AND pp_feas = '$pp_feas' AND pp_design = '$pp_design' AND pp_construction = '$pp_construction' AND pp_other = '$pp_other' ORDER BY provider_name";
} else {
$sql = "SELECT * FROM providers WHERE statewide = '$statewide' AND adams = '$adams' AND asotin = '$asotin' AND benton = '$benton' AND chelan = '$chelan' AND clallam = '$clallam' AND clark = '$clark' AND columbia = '$columbia' AND cowlitz = '$cowlitz' AND douglas = '$douglas' AND ferry = '$ferry' AND franklin = '$franklin' AND garfield = '$garfield' AND grantt = '$grantt' AND grays_harbor = '$grays_harbor' AND island = '$island' AND jefferson = '$jefferson' AND king = '$king' AND kitsap = '$kitsap' AND kittitas = '$kittitas' AND klickitat = '$klickitat' AND lewis = '$lewis' AND lincoln = '$lincoln' AND mason = '$mason' AND okanogan = '$okanogan' AND pacific = '$pacific' AND pend_oreille = '$pend_oreille' AND pierce = '$pierce' AND san_juan = '$san_juan' AND skagit = '$skagit' AND skamania = '$skamania' AND snohomish = '$snohomish' AND spokane = '$spokane' AND stevens = '$stevens' AND thurston = '$thurston' AND wahkiakum = '$wahkiakum' AND walla_walla = '$walla_walla' AND whatcom = '$whatcom' AND whitman = '$whitman' AND yakima = '$yakima' AND el_all = '$el_all' AND el_cities_towns = '$el_cities_towns' AND el_county = '$el_county' AND el_state = '$el_state' AND el_federal = '$el_federal' AND el_nat = '$el_nat' AND el_nonprofit = '$el_nonprofit' AND el_private = '$el_private' AND el_individual = '$el_individual' AND el_spd = '$el_spd' AND el_ed_inst = '$el_ed_inst' AND el_other = '$el_other' AND sc_all = '$sc_all' AND sc_grants = '$sc_grants' AND sc_loans_pri = '$sc_loans_pri' AND sc_gloans = '$sc_gloans' AND sc_ta = '$sc_ta' AND sc_other = '$sc_other' AND ced_all = '$ced_all' AND ced_disaster = '$ced_disaster' AND ced_bus_incubators = '$ced_bus_incubators' AND ced_historic_pres = '$ced_historic_pres' AND ced_housing = '$ced_housing' AND ced_industrial = '$ced_industrial' AND ced_nr = '$ced_nr' AND ced_pub_safety = '$ced_pub_safety' AND ced_research = '$ced_research' AND ced_rural_plan = '$ced_rural_plan' AND ced_sust_dev = '$ced_sust_dev' AND ced_urb_plan = '$ced_urb_plan' AND ced_other = '$ced_other' AND comm_all = '$comm_all' AND comm_dl = '$comm_dl' AND comm_telemed = '$comm_telemed' AND comm_broadband = '$comm_broadband' AND comm_phone = '$comm_phone' AND comm_pt = '$comm_pt' AND comm_telemetry = '$comm_telemetry' AND comm_other = '$comm_other' AND ed_all = '$ed_all' AND ed_childcare = '$ed_childcare' AND ed_k12 = '$ed_k12' AND ed_he = '$ed_he' AND ed_lib = '$ed_lib' AND ed_comm_ed = '$ed_comm_ed' AND ed_research = '$ed_research' AND ed_other = '$ed_other' AND ener_all = '$ener_all' AND ener_alt = '$ener_alt' AND ener_coal = '$ener_coal' AND ener_elec = '$ener_elec' AND ener_hydro = '$ener_hydro' AND ener_nuclear = '$ener_nuclear' AND ener_solar = '$ener_solar' AND ener_wind = '$ener_wind' AND ener_other = '$ener_other' AND ener_bio = '$ener_bio' AND env_all = '$env_all' AND env_ag = '$env_ag' AND env_brownfields = '$env_brownfields' AND env_ener_eff = '$env_ener_eff' AND env_forestry = '$env_forestry' AND env_green_bldg = '$env_green_bldg' AND env_grazing = '$env_grazing' AND env_landscaping = '$env_landscaping' AND env_landuse = '$env_landuse' AND env_riparian = '$env_riparian' AND env_fish_wild = '$env_fish_wild' AND env_oil_rest = '$env_oil_rest' AND env_wetlands = '$env_wetlands' AND env_sp = '$env_sp' AND env_other = '$env_other' AND pf_all = '$pf_all' AND pf_art = '$pf_art' AND pf_comm_centers = '$pf_comm_centers' AND pf_ad_al = '$pf_ad_al' AND pf_fs = '$pf_fs' AND pf_ps = '$pf_ps' AND pf_pj = '$pf_pj' AND pf_pl = '$pf_pl' AND pf_health_fac = '$pf_health_fac' AND pf_mun_bldgs = '$pf_mun_bldgs' AND pf_museums = '$pf_museums' AND pf_other = '$pf_other' AND rec_all = '$rec_all' AND rec_greenbelt = '$rec_greenbelt' AND rec_outdoor = '$rec_outdoor' AND rec_parks = '$rec_parks' AND rec_sports = '$rec_sports' AND rec_trails = '$rec_trails' AND rec_other = '$rec_other' AND rec_play = '$rec_play' AND trans_all = '$trans_all' AND trans_air = '$trans_air' AND trans_bridges = '$trans_bridges' AND trans_marine = '$trans_marine' AND trans_offroad = '$trans_offroad' AND trans_pt = '$trans_pt' AND trans_ped_bike = '$trans_ped_bike' AND trans_parking = '$trans_parking' AND trans_pub_lighting = '$trans_pub_lighting' AND trans_streets = '$trans_streets' AND trans_rail = '$trans_rail' AND trans_other = '$trans_other' AND wm_all = '$wm_all' AND wm_hazard = '$wm_hazard' AND wm_recycling = '$wm_recycling' AND wm_solid_waste = '$wm_solid_waste' AND wm_waste_water = '$wm_waste_water' AND wm_other = '$wm_other' AND water_all = '$water_all' AND water_dams = '$water_dams' AND water_irr = '$water_irr' AND water_storm = '$water_storm' AND water_drinking = '$water_drinking' AND water_drought = '$water_drought' AND water_flood = '$water_flood' AND water_other = '$water_other' AND pp_all = '$pp_all' AND pp_planning = '$pp_planning' AND pp_feas = '$pp_feas' AND pp_design = '$pp_design' AND pp_construction = '$pp_construction' AND pp_other = '$pp_other' ORDER BY provider_name";
}

$numresults = mysql_query($sql);
$numrows = mysql_num_rows($numresults);

if ($numrows == 0) {
$display_block .= "<h4>Results</h4>";
$display_block .= "<p>Sorry, your search returned 0 results. Please <a href=\"search.php\">go back and try again</a>.</p>";
}

if (empty($s)) {
$s=0;
}

//get results
$sql .= " limit $s,$limit";
$result = mysql_query($sql,$connection) or die(mysql_error());

//start displaying results
$display_block .= "<h4>Results</h4>";
$count = 1 +$s;

//variable for changing background color of rows returned
$num_rows = 0;

$display_block .= "
<table border=\"1px\">
<tr valign=\"top\">
<td align=\"center\"><strong>PROGRAM</strong></td>
<td align=\"center\"><strong>DESCRIPTION</strong></td>
<td align=\"center\"><strong>CONTACT</strong></td>
<td align=\"center\"><strong>PHONE</strong></td>
</tr>";

//display results
while ($row = mysql_fetch_array($result)) {
if ($num_rows % 2 == 0) {
$bgcolor = '#ffffd7';
} else {
$bgcolor = '#eeeeee';
}
$num_rows--;

$id = $row['id'];
$provider_name = $row['provider_name'];
$contact_fname = $row['contact_fname'];
$contact_lname = $row['contact_lname'];
$phone = $row['phone'];
$description = nl2br($row['description']);

$display_block .= "<tr bgcolor=$bgcolor>
<td valign=\"top\"><a href=\"show_details.php?id=$id\">$provider_name</a></td>
<td valign=\"top\">$description</td>
<td valign=\"top\">$contact_fname $contact_lname</td>
<td valign=\"top\">$phone</td>
</tr>";
}

$display_block .= "<tr><td colspan=\"4\">";

$currPage = (($s/$limit) + 1);

//links to other results
if ($s>=1) { //bypass PREV link if s is 0
$prevs =($s-$limit);
$display_block .= "&nbsp;&nbsp; <input type=\"submit\" value=\"&lt;&lt;Prev 10\">";
}

//trying to pass the page variables by POST method
$display_block .= "<input type=\"hidden\" name=\"s\" value=\"$prevs\">";

//calculate number of pages needing links
$pages =intval($numrows/$limit);

//$pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {

//has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

//not last page so give NEXT link
$news=$s+$limit;

$display_block .= "&nbsp;&nbsp; <input type=\"submit\" value=\"Next 10 &gt;&gt;\">";

}

//trying to pass the page variables by POST method
$display_block .= "<input type=\"hidden\" name=\"s\" value=\"$news\">";

$a = $s + ($limit);
if ($a > $numrows) { $a = $numrows; }
$b = $s + 1;
$display_block .= "<p>Showing results $b to $a of $numrows</p>";
$display_block .= "</table></form>";

?>


<?php

echo "$display_block";

?>
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline
Reply With Quote
View Public Profile Visit ffsja's homepage!
 
Old 06-27-2008, 03:39 PM Re: Trouble Querying Database
tripy's Avatar
Fetchez la vache!

Posts: 2,054
Name: Thierry
Location: In the void
Oh my god....

Did you asked what you wanted your query to do ?
Your query don't return anything because there are no rows in your tables that satisfy EVERY criterion you have given.
Of course that OR returns everything, but the simple fact that you have tried shows that you don't have much experience with DB, have you ?

Can you submit your DB schema, please. It would help a lot.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 06-27-2008, 04:02 PM Re: Trouble Querying Database
wayfarer07's Avatar
$frontend->developer

Posts: 1,015
Name: Abel Mohler
Location: Asheville, North Carolina USA
Nothin' like easy readin'
__________________
Go FREELANCE <=|If a donkey eats a melon, it is still a donkey... |=> Hire Me
wayfarer07 is online now
Reply With Quote
View Public Profile
 
Old 06-27-2008, 04:14 PM Re: Trouble Querying Database
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 734
First of all, you're not doing any sort of data validation. This form could be hacked very easily through SQL injection and that means that people could do whatever they want with your database. Besides that, with that many form elements, people are bound to enter the wrong type of information at some point, so you need to check that the data is the kind of data you want. I would kind of question that you need all of those form inputs, but if you do, I'd consider building your query gradually, something like:

PHP Code:
$query "SELECT * FROM providers WHERE";

if (!empty(
$_POST['el_all'])){
     
$el_all mysql_real_escape_string($_POST['el_all']);
     
$query .= " el_all = '$el_all AND' 
}

//and so on 
You could probably build the above into a class or a function of some sort so that you don't have to type it all out, but I really think you should reexamine your design.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 06-27-2008, 04:55 PM Re: Trouble Querying Database
Average Talker

Posts: 19
Data validation? All the fields the user is filling out are checkboxes and radio buttons with values attached to them...do i still need to validate those fields? But yes the client wants all those options, so I can't help that.

Ok, I know the AND and OR statements aren't going to give me what I want...but how do I limit my sql statement to just those checkboxes/radio buttons the user selected (and thus the one's that have values)? that's my real problem. If I could somehow determine which one's had values and which one's were empty I could just include the one's with values in my sql statement and everything would be fine. any ideas?

Also, is there a way to return values from mysql so that apostraphes and so forth are returned correctly?

thanks,
sage
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline
Reply With Quote
View Public Profile Visit ffsja's homepage!
 
Old 06-27-2008, 06:15 PM Re: Trouble Querying Database
Average Talker

Posts: 19
I solved it! Here's the kind of code I used to check the variables:

Code:
$sql = "SELECT * FROM providers WHERE ";
$p=0;
//get records
//if ($all_funders != "") {
    if ($el_all != "") {
        if ($p>0) {
        $sql .= "AND ";
        }
    $sql .= "el_all = '$el_all' ";
    $p++;
    }
    
    if ($el_cities_towns != "") {
        if ($p>0) {
        $sql .= "AND ";
        }
    $sql .= "el_cities_towns = '$el_cities_towns' ";
    $p++;
    }
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline
Reply With Quote
View Public Profile Visit ffsja's homepage!
 
Reply     « Reply to Trouble Querying Database
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.26478 seconds with 12 queries