I am trying to make a simple select query of a mysql database with PHP where two variables are set based on user input. One variable is a month and thus can only be one specific value at a time. The other is dynamic and can either be a specific value or all values in the table. The code looks similar to this:
$var1 = "-1";
if (isset($_GET['Month'])) {
$var1 = $_GET['Month'];
}
$var2 = $_GET['Person'];
if ($var2 == "All") {
$var2 ="('Person 1' or 'Person 2' or 'Person 3')";
}
$query= sprintf("Select * from Table where `Month` = %s and `Person` = %s", GetSQLValueString($var1, "text"), GetSQLValueString($var2, "text"));
When I input a specific person for $var2 I am able to get the correct results, however, when I input All for $var2, the code runs but I return no results.
Actual block of code:
$colname_chainsales = "-1";
if (isset($_GET['Period'])) {
$colname_chainsales = $_GET['Period'];
}
$rep = $_GET['Rep'];
if ($rep == "All") {
$rep = " 1=1 ";
} else {
$rep = " `PSIP` = ".$_GET['Rep'];
}
mysql_select_db($database_PPSC, $PPSC);
$query_chainsales= sprintf("select `PSIP` as `Sales Rep`, `Month`, `chainsales`.`PPSC #` as `PPSC #`, `chainsales`.`Store Name` as `Store Name`, sum(`Sales Less Credits ($)`) as `Sales Less Credits ($)`, sum(`RX Sales`) as `RX Sales`, sum(`Brand RX`) as `Brand RX`,sum(`Brand Rx C2`) as `Brand Rx C2`,sum(`prnr sales`) as `prnr`,sum(`h2h sales`) as `h2h`,sum(`specialty sales`) as `Specialty`,sum(`Brand RX`)-sum(`Brand Rx C2`)- sum(`specialty sales`)-sum(`h2h sales`) as `Net Brand`, sum(`Pro w/SG`) as `Pro w/SG` , sum(`Pro w/o SG`) as `rpro`, SUM(`Pro w/SG`) / (SUM(`RX Sales`)- SUM(`specialty sales`))* 100 AS `Compliance` from `chainsales` inner join `clients` on `chainsales`.`ppsc #`=`clients`.`ppsc #` WHERE `Month` = %s and %s Group by `PPSC #`, `Month` ORDER BY `PSIP`", GetSQLValueString($colname_chainsales, "text"), GetSQLValueString($rep, "text"));
$chainsales = mysql_query($query_chainsales, $PPSC) or die(mysql_error());
$row_chainsales= mysql_fetch_assoc($chainsales);
$totalRows_chainsales = mysql_num_rows($chainsales);