php中未定义的索引错误虽然我定义了它。为什么?

时间:2017-09-10 17:01:46

标签: php mysql

我是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);
 }
 ?>

错误

  

注意:未定义的索引:第3行的C:\ xampp \ htdocs \ getServices \ employeesInfo.php中的城市

     

注意:未定义的索引:第4行的C:\ xampp \ htdocs \ getServices \ employeesInfo.php中的town

     

警告:mysqli_fetch_array()要求参数1为mysqli_result,第17行的C:\ xampp \ htdocs \ getServices \ employeesInfo.php中给出布尔值   { “结果”:[]}

1 个答案:

答案 0 :(得分: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";
}

?>