2017-09-10 112 views
0

我新的PHP所以請指導,不讓它重複,因爲我無法擺脫以前的solutions.My PHP代碼解決方案如下在php中未定義的索引錯誤,雖然我定義它。爲什麼?

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$city = $_GET['city']; 
$town = $_GET['town']; 
//$skills=$_POST['skills']; 
require_once('DbConnect.php'); 
//Creating sql query 
$sql = "SELECT FROM employees where city='".$city."' and town='".$town."'"; 

//getting result 
$r = mysqli_query($con,$sql); 

//creating a blank array 
$result = array(); 

//looping through all the records fetched 
while($row = mysqli_fetch_array($r)){ 

//Pushing name and id in the blank array created 
array_push($result,array(
"name"=>$row['name'], 
"phone"=>$row['phone'], 
"skills"=>$row['skills'] 
)); 
} 

//Displaying the array in json format 
echo json_encode(array('result'=>$result)); 

mysqli_close($con); 
} 
?> 

錯誤

給出注意:未定義指數:城市在C:\ XAMPP \ htdocs中\ getServices \ employeesInfo.php上線3

注意:未定義指數:鎮在C:\ XAMPP \ htdocs中\ getServices \ employeesInfo.php 4號線

警告:mysqli_fetch_array()預計參數1被mysqli_result,在C中給出布爾:\ XAMPP \ htdocs中\ getServices \ employeesInfo.php上線17 { 「結果」:[]}

+2

將您的SQL查詢更改爲'$ sql =「SELECT * FROM employees ...' –

+1

什麼是URL或表單?你開放SQL注入,參數化。只檢查請求方法是不夠的。 – chris85

+1

除了顯而易見的SQL注入問題,如果沒有在您的URL中定義城市或城鎮的變量,那麼您將得到此錯誤 –

回答

1

需要通過做選擇一個或多個列,例如選擇所有SELECT * FROM..,查詢應該是這樣的

$sql = "SELECT * FROM employees where city='".$city."' and town='".$town."'"; 

Update_Code:

<?php 

if($_SERVER['REQUEST_METHOD']=='GET' && !empty($_GET['city']) && !empty($_GET['town'])){ 

$city = $_GET['city']; 
$town = $_GET['town']; 

//$skills=$_POST['skills']; 
require_once('DbConnect.php'); 

//Creating sql query 
$sql = "SELECT * FROM employees where city='".$city."' and town='".$town."'"; 

//getting result 
$r = mysqli_query($con,$sql); 

//creating a blank array 
$result = array(); 

//looping through all the records fetched 
while($row = mysqli_fetch_array($r)){ 

//Pushing name and id in the blank array created 
array_push($result,array(
"name"=>$row['name'], 
"phone"=>$row['phone'], 
"skills"=>$row['skills'] 
)); 
} 

//Displaying the array in json format 
echo json_encode(array('result'=>$result)); 

mysqli_close($con); 
} 
else{ 

$message = " Fill all the details First"; 

} 

if(isset($message) && !empty($message)){ 

echo "$message"; 
} 

?>