2015-11-17 35 views
2

我想通過選擇treecondition列來顯示treeid,treelatitude和treelongitude。未答覆的PHP - 選擇treecondition列並顯示其他3列

當我POST「健康」,我能夠顯示treeid 4,6 & 7,但如果我POST「平衡」不能得到treeid 1 & 8.我怎樣才能得到它?

在我下面的代碼,我只能能夠顯示「健康」,但無法顯示「平衡」

請給我在我的代碼獲得錯誤

Treeid treelatitude treelongitude treeCondition 
    1  12.33   17.22   Balanced 
    2  12.33   17.22   Healthy 
    3  12.33   17.33   Dieseased 
    4  13.44   17.55   Healthy 
    5  11.32   17.66   Imbalanced 
    6  12.33   18.33   Healthy 
    7  14.44   18.44   Healthy 
    8  11.22   17.22   Balanced 

<?php 

define('__ROOT__', dirname(dirname(__FILE__))); 
require_once(__ROOT__.'/public_html/Config.php'); 


// Connecting to mysql database 
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); 

// json response array 
$response = array(); 


    $sq = "SELECT treeid, treelatitude, treelongitude FROM tree WHERE (treecondition = 'Healthy','Balanced')"; 

if (isset($_POST['treecondition'])) { 

// receiving the post params 

$treecondition = $_POST['treecondition']; 

// get the tree details for google map marker 
if($stmt = $mysqli->query($sq)){ 

    if ($stmt->num_rows) { 


     while($tree = $stmt->fetch_assoc()) { 

     $treeItem = array(); 

     $treeItem["treeid"] = $tree['treeid']; 
     $treeItem["treelatitude"] = $tree['treelatitude']; 
     $treeItem["treelongitude"] = $tree['treelongitude']; 
     $response[] = $treeItem; 

    } echo json_encode($response); 

    } 

    }else { 
    // user is not found with the credentials 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Tree list view credentials are wrong. Please try again!"; 
echo json_encode($response); 
} 
$mysqli->close(); 
} 

回答

0

這應該正是幫助被WHERE treecondition IN ('Healthy','Balanced')";

編輯

試試這個:

$treecondition = $mysqli->real_escape_string($_POST['treecondition']);

"SELECT treeid, treelatitude, treelongitude FROM tree WHERE treeCondition='{$treecondition}'"

我們已經使用real_escape_string逃避直接張貼的字符。這會防止SQL注入。

而且MySQL是大小寫敏感的,請使用treeCondition代替treecondition

+0

它不工作!它正在採取所有的樹ID – Bharat

+0

你使用MYSQL? –

+0

當你發佈「平衡」時,你是否仍然「健康」? –

0

查詢應該是:

$sq = "SELECT treeid, treelatitude, treelongitude FROM tree 
     WHERE treecondition = '".$treecondition."'"; 

你也應該添加上面的查詢下面$treecondition = $_POST['treecondition'];線。

+0

它不工作!它採取所有樹ID – Bharat

+0

@Bharat:請檢查我的更新的答案 – AnkiiG

+0

我已經使用該行。 – Bharat