1

I am new to php so please guide and don't make it duplicate because i can't get solution from previous solutions.My php code is given below

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

ERROR

Notice: Undefined index: city in C:\xampp\htdocs\getServices\employeesInfo.php on line 3

Notice: Undefined index: town in C:\xampp\htdocs\getServices\employeesInfo.php on line 4

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\getServices\employeesInfo.php on line 17 {"result":[]}

10
  • 2
    Change your SQL query to $sql = "SELECT * FROM employees ... Commented Sep 10, 2017 at 17:04
  • 1
    Whats the URL or form? You are open to SQL injections, parameterize. Just checking the request method isnt enough. Commented Sep 10, 2017 at 17:05
  • 1
    Besides the obvious SQL injection issues, if there are no get vars for city or town defined in your URL, then you will get this error Commented Sep 10, 2017 at 17:06
  • Use die method with $_GET to see the contents and verify if city & town keys are present. Commented Sep 10, 2017 at 17:07
  • 1
    @DeepanshSachdeva OP will need print_r as well, die alone cant print array contents. Commented Sep 10, 2017 at 17:10

1 Answer 1

2

You need to select one or more columns, for example by doing select all SELECT * FROM.., the query would look like this

$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";
}

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.