2017-08-06 49 views
0

我試圖使用選擇選項標籤,使得如果我選擇'全部'在選擇選項標籤,然後每個該列的數據應該被提取。通過選擇選項標籤從數據庫提取數據,這樣當選擇'所有'數據庫應該選擇'*'

以下給出的是我的php的代碼。由於

if(isset($_POST['submit'])) { 
    $area = $_POST['city']; 
    if(isset($_POST['work']!== 'all')) 
     {$work = $_POST['work']; } 
    else {$work = '*';} ; 

    $sel2 = "SELECT * FROM `userdata` WHERE `area`='".$area."' AND `work`='".$work."'" ; 
    $res2 = mysqli_query($con,$sel2); 
+0

你嘗試過什麼迄今爲止變量?查詢是什麼樣的? – Qirel

回答

0

既然你是想獲取任何與工作相關的所有數據時work等於all,你沒有明確提到,在SQL查詢。僅當您想要獲取特定的工作相關數據時,提及。所以,你的代碼應該是這樣的:

if(isset($_POST['submit'])) { 
    $area = $_POST['city']; 
    $sel2 = "SELECT * FROM `userdata` WHERE `area`='".$area."'"; 
    if(isset($_POST['work']) && $_POST['work'] !== 'all') 
     $sel2 .= " AND `work`='".$_POST['work']."'"; 
    $res2 = mysqli_query($con,$sel2); 

旁註:瞭解prepared statement因爲現在你的查詢是容易受到SQL注入攻擊。另見how you can prevent SQL injection in PHP

+0

謝謝Rajdeep!有用。 – fahad

0

設置這樣

if(isset($_POST['submit'])) { 
    $area = $_POST['city']; 
    if(isset($_POST['work']!== 'all')) 
$data=addslashes($_POST['work']); 
     {$work = "AND `work`='".$data."'"; 
} 
    else {$work = ""; 
} 
///your query should be like this 
$sel2 = "SELECT * FROM `userdata` WHERE `area`='".$area."' ".$work.""; 
//////so if work is not specified then you don't need to select column WORK in the query. So it works as select * from userdata where area='".$area."';"; 

    $res2 = mysqli_query($con,$sel2);