2014-01-23 21 views
0

我試圖檢索動態查詢PHP頁面。首頁通過post方法傳遞城市或id下一頁的值。如何使表中的所有查詢的動態php頁面

我知道這裏sql注入可以完成。 但我使用它在私人網絡和每個用戶會話進行跟蹤。 和table1受保護。

表看起來是這樣的:

TABLE1

----------------------------------------------------- 
1  select * from tablename1 where ID = $id 
2  select * from tablename1 where city = $city 
2  select * from tablename1 where ID = $id and city = $city 
----------------------------------------------------- 

現在我跑頁包含代碼:

$id = $_POST["id"]; 
    $city= $_POST["city"]; 
    $tabledata = mysql_query("SELECT * FROM table1 "); 
    $a=mysql_fetch_row($tabledata); 

    $query_table=$a['1']; // retrieve sql query from table1 which stored in 2nd column 
    echo " $query_table"; // display query 
    $result = mysql_query($query_table); 

    while ($field = mysql_fetch_row($result)) 
    { 
     echo "<td> $field[1] </td>"; 
    } 

現在的問題是,我不能得到結果。 $result是空的?

如何通過$city$id值查詢?

但是,如果我從TABKE1刪除WHERE條件,那麼它運作良好。那麼,我怎樣才能讓WHERE條件傳遞參數呢?

我該怎麼做。這裏的任何其他選項調用存儲在1個表查詢,如果你想與ID或市

mysql_query("SELECT * FROM table1 WHERE ID = '$id' OR city = '$city'"); 

結果處理它的其他頁面和oher表

+0

你再往前走之前,強制性的建議,[**請勿使用變化新代碼中的'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

0

table1中查詢應該是這樣的

----------------------------------------------------- 
1  select * from tablename1 where ID = '".$id."' 
2  select * from tablename1 where city = '".$city."' 
3  select * from tablename1 where ID = '".$id."' and city = '".$city."' 
----------------------------------------------------- 

,並與鱈魚繼續進行它會告訴你結果。

選項2:

表1:

----------------------------------------------------- 
1  select * from tablename1 where ID = 
2  select * from tablename1 where city = 
----------------------------------------------------- 

和這樣

$query_table=$a['1']." '".$id."'" ; 

$query_table=$a['2']." '".$city."'" ; 

OPTION 3 PHP代碼變化:

表1:

----------------------------------------------------- 
1  select * from tablename1 

----------------------------------------------------- 

和PHP代碼這樣

$query_table=$a['1']." where ID = '".$id."'" ; 

$query_table=$a['1']." where ID = '".$id."' AND city = '".$city."' " ; 
+0

不能正常工作...顯示爲 select * from tablename1 where ID ='「。$ id。」'not value of $ id –

+0

其工作,但對於「select * from tablename1 where ID = And City = 「 不管用 。給我解決這個問題。 –

+0

檢查我編輯的答案,你不能給這樣的查詢'select * from tablename1 where ID = and City =' – krishna

0

上,如果你想用這兩個強制性

mysql_query("SELECT * FROM table1 WHERE ID = '$id' AND city = '$city'"); 
+1

仔細閱讀。 TABLE1只包含查詢。並用於其他表 –

相關問題