2012-07-25 64 views
0

所以我有一個表單有3個下拉列表與不同的標準,一個是搜索月(mes),年(ano)和類型(tipo_id)。(Php)用多個選項創建一個搜索查詢,但MONTHNAME和YEAR

我想做什麼,並感謝在這裏的許多問題,我能夠推進到這個地方,是當你從3下拉選擇2或1的東西,它會顯示一個表提到有關它的信息,我能做到這一點,但只是tipo_id,我認爲這個問題是在使用MONTHNAME和年度途中用我的代碼,我想知道我怎樣才能使這項工作,在此先感謝

// 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>"; 

回答

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); 
} 
+0

它沒有工作,它只是顯示獲取我從「tipo_id」中選擇的所有東西......也許我錯過了一些東西 – Fabiotp91 2012-07-25 11:37:55

+0

@ Fabiotp91你確定你正在處理正確的MySQL結果嗎? Omesh的代碼會生成一個查詢,如「SELECT ... WHERE 1 OR ... AND OR ... AND OR ...」,它不能返回任何內容,因爲它在語法上不正確。另一種可能性是$ _POST ['mes']和$ _POST ['ano']未設置或爲空。 – lafor 2012-07-25 12:00:01

+0

我編輯我的帖子,告訴我我的完整代碼 – Fabiotp91 2012-07-25 12:09:10

0

內爆後$searches you en最多可以查詢3 WHERE關鍵字。去掉前兩個WHERE應該可以做到。

+0

一定有其他錯誤,最初我沒有他們,但即使如此,它只是從「 tipo_id「而不尊重」ano「和」mes「 – Fabiotp91 2012-07-25 11:39:08