2017-09-10 10 views
0

Ich bin so php bitte führen und machen es nicht duplizieren, weil ich nicht Lösung aus früheren solutions.My PHP-Code bekommenUndefinierter Indexfehler in PHP, obwohl ich es definiert habe. Warum?

unter

<?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 gegeben

notice: Undefined index: Stadt in C: \ xampp \ htdocs \ getServices \ employeesInfo.php auf Linie 3

notice: Undefined index: Stadt in C: \ xampp \ htdocs \ getServices \ employeesInfo.php auf Linie 4

Warning: mysqli_fetch_array() erwartet 1 Parameter MySQLi_Result, boolean Bereichen gemäß c: 17 { "result" []} \ xampp \ htdocs \ getServices \ employeesInfo.php on line

+2

Ändern Sie Ihre SQL-Abfrage in '$ sql =" SELECT * FROM Mitarbeiter ... " –

+1

Wie lautet die URL oder das Formular? Sie sind offen für SQL-Injektionen, parametrisieren. Nur die Anfrage-Methode zu überprüfen ist nicht genug. – chris85

+1

Abgesehen von den offensichtlichen SQL-Injection-Problemen, wenn in Ihrer URL keine get vars für Stadt oder Ort definiert sind, erhalten Sie diesen Fehler –

Antwort

1

Sie brauchen eine oder mehr Spalten auswählen, zum Beispiel durch tun wählen Sie allSELECT * FROM.., die Abfrage wie dies

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

update_code aussehen:

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

?> 
Verwandte Themen