I have a form with these fields:
<select multiple name="name[]">
<select multiple name="jobnumber[]">
<select multiple name="client[]">
<select multiple name="stage[]">
and a couple of non array fields.
The form goes to a script on the next page:
#function to change American date to English
function englishToAmericanDate($str)
{
$elements = explode("-", $str);
$americandate = $elements[2]."-".$elements[1]."-".$elements[0];
return $americandate;
}
$americandate = englishToAmericanDate($firstdate);
$americandate2 = englishToAmericanDate($lastdate);
$db = mysql_connect("localhost", "root"
;
mysql_select_db("acwh",$db);
if ($description == ""
{$description = '%';}
if ($timetaken == ""
{$timetaken = '%';}
# This is a recursive function, which either adds a value into a global array
# for use in the select statement, or recurses if the value is an
# array itself and does the same thing.
function DisassembleArray($key, $val)
{
global $queries;
if((!is_array($val)) && ($val != ""
)
{
$queries[] = "$key = '$val'";
} else
{
foreach($val as $foo => $real_val)
{
DisassembleArray($key, $real_val);
}
}
}
# These are the names of the form fields, and they also correspond
# to field names in the table that we want to look through.
$valid_fields = array("name", "jobnumber", "client", "stage"
;
$queries = array();
foreach($_POST as $key => $val)
{
if(!in_array($key, $valid_fields)) { continue; }
DisassembleArray($key, $val);
}
# Start of the query.
$query = "SELECT * FROM timesheets where description LIKE '%$description%' AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '".$americandate."' AND '".$americandate2."'";
if(count($queries) > 0)
# If something's been selected, we append it to the query.
{
$query .= " and " . implode(" or ", $queries);
}
$query .= " order by entrydate desc";
$result = mysql_query ($query,$db);
If on the form page I choose 'James' and 'Pete' from the staffname box and 'acd000' and 'acd001' from the jobnumber box and search by them and I echo out the SQL statement I get:
SELECT * FROM timesheets where description LIKE '%%%' AND timetaken LIKE '%%%' AND entrydate BETWEEN '1-1-1' AND '03-05-19' and name = 'jamesf4218' or name = 'Pete' or jobnumber = 'acd000' or jobnumber = 'acd001' order by entrydate desc
When what I REALLY want is:
SELECT * FROM timesheets where description LIKE '%%%' AND timetaken LIKE '%%%' AND entrydate BETWEEN '1-1-1' AND '03-05-19' and (name = 'jamesf4218' or name = 'Pete') AND (jobnumber = 'acd000' or jobnumber = 'acd001') order by entrydate desc
How do I change the above code to get this?
Many thanks
James
<select multiple name="name[]">
<select multiple name="jobnumber[]">
<select multiple name="client[]">
<select multiple name="stage[]">
and a couple of non array fields.
The form goes to a script on the next page:
#function to change American date to English
function englishToAmericanDate($str)
{
$elements = explode("-", $str);
$americandate = $elements[2]."-".$elements[1]."-".$elements[0];
return $americandate;
}
$americandate = englishToAmericanDate($firstdate);
$americandate2 = englishToAmericanDate($lastdate);
$db = mysql_connect("localhost", "root"

mysql_select_db("acwh",$db);
if ($description == ""

{$description = '%';}
if ($timetaken == ""

{$timetaken = '%';}
# This is a recursive function, which either adds a value into a global array
# for use in the select statement, or recurses if the value is an
# array itself and does the same thing.
function DisassembleArray($key, $val)
{
global $queries;
if((!is_array($val)) && ($val != ""

{
$queries[] = "$key = '$val'";
} else
{
foreach($val as $foo => $real_val)
{
DisassembleArray($key, $real_val);
}
}
}
# These are the names of the form fields, and they also correspond
# to field names in the table that we want to look through.
$valid_fields = array("name", "jobnumber", "client", "stage"

$queries = array();
foreach($_POST as $key => $val)
{
if(!in_array($key, $valid_fields)) { continue; }
DisassembleArray($key, $val);
}
# Start of the query.
$query = "SELECT * FROM timesheets where description LIKE '%$description%' AND timetaken LIKE '%$timetaken%' AND entrydate BETWEEN '".$americandate."' AND '".$americandate2."'";
if(count($queries) > 0)
# If something's been selected, we append it to the query.
{
$query .= " and " . implode(" or ", $queries);
}
$query .= " order by entrydate desc";
$result = mysql_query ($query,$db);
If on the form page I choose 'James' and 'Pete' from the staffname box and 'acd000' and 'acd001' from the jobnumber box and search by them and I echo out the SQL statement I get:
SELECT * FROM timesheets where description LIKE '%%%' AND timetaken LIKE '%%%' AND entrydate BETWEEN '1-1-1' AND '03-05-19' and name = 'jamesf4218' or name = 'Pete' or jobnumber = 'acd000' or jobnumber = 'acd001' order by entrydate desc
When what I REALLY want is:
SELECT * FROM timesheets where description LIKE '%%%' AND timetaken LIKE '%%%' AND entrydate BETWEEN '1-1-1' AND '03-05-19' and (name = 'jamesf4218' or name = 'Pete') AND (jobnumber = 'acd000' or jobnumber = 'acd001') order by entrydate desc
How do I change the above code to get this?
Many thanks
James