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":[]}

Community
  • 1
  • 1
Abdul Mateen
  • 1,139
  • 1
  • 14
  • 21
  • 2
    Change your SQL query to `$sql = "SELECT * FROM employees ...` – Rajdeep Paul Sep 10 '17 at 17:04
  • 1
    Whats the URL or form? You are open to SQL injections, parameterize. Just checking the request method isnt enough. – chris85 Sep 10 '17 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 – Mark Baker Sep 10 '17 at 17:06
  • Use `die` method with `$_GET` to see the contents and verify if city & town keys are present. – Deepansh Sachdeva Sep 10 '17 at 17:07
  • 1
    @DeepanshSachdeva OP will need `print_r` as well, `die` alone cant print array contents. – chris85 Sep 10 '17 at 17:10
  • @RajdeepPaul thanks but i am still getting Undefined index error on"city & town".please explain – Abdul Mateen Sep 10 '17 at 17:11
  • Also would you ever have a city and town; aren't those the same thing, just different government structures? – chris85 Sep 10 '17 at 17:13
  • See @chris85's [comment there](https://stackoverflow.com/questions/46143287/undefined-index-error-in-php-although-i-defined-it-why?noredirect=1#comment79248357_46143287). Also, do `var_dump($_GET);` and see what you have in there. – Rajdeep Paul Sep 10 '17 at 17:13
  • You're already using an API that supports **prepared statements** with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`mysqli::prepare()`](http://php.net/mysqli.prepare) and [`mysqli_stmt::bind_param()`](http://php.net/mysqli-stmt.bind-param). – Qirel Sep 10 '17 at 17:34
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Sep 10 '17 at 17:34

1 Answers1

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

?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36