0

所以我有一个表单,它有 3 个具有不同条件的下拉列表,一个是搜索月份 (mes)、年份 (ano) 和类型 (tipo_id)。

我想做的事,多亏了这里的许多问题,我才能进入这个地方,当你从 3 个下拉列表中选择一些东西,2 或 1 时,它会显示一个引用有关它的信息的表格,我可以做它,但只是对于tipo_id,我认为问题在于使用MONTHNAME和YEAR的方式在我的代码中使用,我想知道我怎样才能使它工作,在此先感谢

 // Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$mes = mysql_real_escape_string($_POST['mes']);
$ano = mysql_real_escape_string($_POST['ano']);
$tipo = mysql_real_escape_string($_POST['tipo_id']);


$meses = array( 
  Janeiro => 'January', 
  Fevereiro =>'February', 
  Março =>'March', 
  Abril =>'April', 
  Maio =>'May', 
  Junho =>'June', 
  Julho => 'July', 
  Agosto =>'August', 
  Setembro =>'September', 
  Outubro =>'October', 
  Novembro =>'November', 
  Dezembro =>'December' 
); 


$sql4 = "SELECT * FROM fluxo ";
$searches = array();
if ($mes  != '') $searches[] = " OR MONTHNAME(data) = '".$meses[$mes]."'";
if ($ano  != '') $searches[] = " OR YEAR(data) = '".$ano."'";
if ($tipo != '') $searches[] = " OR tipo = '".$tipo."'";
if (count($searches) > 0) {
   $sql4 .= " WHERE 1 " . implode(" AND ", $searches);

echo "sql4=$sql4\n";



//$sql4 = "SELECT tipo, movimento, valor, data FROM fluxo WHERE tipo = '".$tipo."' AND MONTHNAME(data) = '".$meses[$mes]."' AND YEAR(data) = '".$ano."' ";
//$result4=mysql_query($sql4); 

$result5 = mysql_query('SELECT SUM(valor) AS value_sum FROM fluxo GROUP BY tipo having count(tipo)>1'); 
$row5 = mysql_fetch_assoc($result5); 
$sum5 = $row5['value_sum'];

$n=1; 

echo "<p>Os seus resultados:<p>";

echo "<table border=0>";
echo "<tr bgcolor='#CCFFCC'>";
echo "<td style='width: 100px;'>Tipo</td>";
echo "<td style='width: 100px;'>Movimento</td>";
echo "<td style='width: 100px;'>Valor</td>";
echo "<td style='width: 100px;'>Data</td>";
echo "</tr>";

while($row = mysql_fetch_array($result4)){ 
    echo "<tr bgcolor='#CCCCCC'>";
    echo "<td style='width: 100px;'>".$row['tipo']."</td>";
    echo "<td style='width: 100px;'>".$row['movimento']."</td>";
    echo "<td style='width: 100px;'>".$row['valor']."</td>";
    echo "<td style='width: 100px;'>".$row['data']."</td>";
    echo "</tr>";

}

echo "</table>";
4

2 回答 2

0

这应该有效:

$sql4 = "SELECT * FROM fluxo ";
$searches = array();
if ($mes  != '') $searches[] = " MONTHNAME(data) = '".$meses[$mes]."'";
if ($ano  != '') $searches[] = " YEAR(data) = '".$ano."'";
if ($tipo != '') $searches[] = " tipo = '".$tipo."'";
if (count($searches) > 0) {
   $sql4 .= " WHERE 1 " . implode(" AND ", $searches);
}
于 2012-07-25T11:33:51.460 回答
0

内爆后$searches,您最终会得到一个最多包含 3 个WHERE关键字的查询。删除前两个WHEREs 应该可以解决问题。

于 2012-07-25T11:37:45.607 回答