2017-09-10 86 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"; 
} 

?>