Jump to content

SQL Query help


Andrew R

Recommended Posts

How would I set my forms and the SQL query (below) so if I didn't want to view by a specific option (aircraft, Departing_City and Arriving_City) the users could click Any from the drop down box and view all the results from the database?

[code]<select name="search" id="search">
  <option value="">Any</option>
          <?php print $select_options ; ?> </select>[/code]

[code]<select name="search2" id="search2">
  <option value="">Any</option>
          <?php print $select_options2 ; ?> </select>[/code]

[code]<select name="search3" id="search3">
          <option value="">Any</option>
          <?php print $select_options3 ; ?> </select>[/code]

[code]
$search=$_POST["search"];
$search2=$_POST["search2"];
$search3=$_POST["search3"];

$query_flights = "SELECT * FROM Schedules WHERE Aircraft = '$search' AND Departing_City = '$search2' AND Arriving_City = '$search3'";[/code]
Link to comment
https://forums.phpfreaks.com/topic/18993-sql-query-help/
Share on other sites

I think the following covers all situations:
[code]<?php
$search =$_POST["search"];
$search2=$_POST["search2"];
$search3=$_POST["search3"];

$dep = " Departing_City = '$search2' ";
$arr = " Arriving_City = '$search3' ";
$sql = "SELECT * FROM Schedules ";
$sub = "";

if ($search2 && !$search3)
  $sub .= "WHERE $dep";
if ($search2 && $search3)
  $sub .= "WHERE $dep AND $arr";
if ($search3 && !$search2)
  $sub .= "WHERE $arr";
if ($search)
  $sub  = "";

$query_flights = $sql.$sub;
echo $query_flights;
?>[/code]

Ronald  8)
Link to comment
https://forums.phpfreaks.com/topic/18993-sql-query-help/#findComment-82139
Share on other sites

I don't  say that you are wrong, but you can't fuss about samples having no sanitizing input.

People show parts of their code because they have a problem. Since it many times involves very much code, they only show snippets. I myself use a 50 lines class to sanitize input, but I would'nt show that in every question I pose to the forum.

Ronald  8)
Link to comment
https://forums.phpfreaks.com/topic/18993-sql-query-help/#findComment-82147
Share on other sites

Read :)
http://www.unixwiz.net/techtips/sql-injection.html
http://www.governmentsecurity.org/articles/SQLinjectionBasicTutorial.php
And many more on google:
http://www.google.com/search?hl=en&lr=&q=sql+injection+php


For escaping, I suggest you to use the function I added below rather than mysql_real_escape_string(). Because on some servers magic quotes are on and on some it's off.
Here:
[code]<?php
function sql_quote($value)
{
    if(get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }
   
    if(function_exists("mysql_real_escape_string"))
    {
        $value = mysql_real_escape_string($value);
    }
    else
    {
        $value = addslashes($value);
    }
    return $value;
}
?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/18993-sql-query-help/#findComment-82175
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.