1
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");

include("global.php");

$conn = new mysqli(server, dbuser, dbpw, db);

$user = $_GET['user'];
//$customer = $_GET['customer'];
$querystring = "";
$querystring = "SELECT email, password, firstname, lastname from user ";
$result = $conn->query($querystring);



$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"email":"'  . $rs["email"] . '",';
    $outp .= '"password":"'  . $rs["password"] . '",';
    $outp .= '"firstname":"'   . $rs["firstname"] . '",';   
    $outp .= '"lastname":"'   . $rs["lastname"]. '"}';
}
$outp .="]";

$conn->close();

echo($outp);
?>

This is the PHP code in the database and need to use get method. the error shows undefined index : $user = $_GET['user']; when I run it shows this error. How to fix it?

akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24
yangmy
  • 13
  • 3

3 Answers3

0

I added the IF condition in below code you can check it. By adding this condition you are able to solve the undefine index issue

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");

include("global.php");

$conn = new mysqli(server, dbuser, dbpw, db);
if(!isset($_GET['user']) && empty($_GET['user']))
return false;// this will send back if data or index is not found and also solve the undefine index problem
$user = $_GET['user'];
//$customer = $_GET['customer'];
$querystring = "";
$querystring = "SELECT email, password, firstname, lastname from user ";
$result = $conn->query($querystring);



$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"email":"'  . $rs["email"] . '",';
    $outp .= '"password":"'  . $rs["password"] . '",';
    $outp .= '"firstname":"'   . $rs["firstname"] . '",';   
    $outp .= '"lastname":"'   . $rs["lastname"]. '"}';
}
$outp .="]";

$conn->close();

echo($outp);
?>
pawansgi92
  • 1,065
  • 13
  • 32
0

You can fix this issue by add isset in $_GET['user'] and check its empty or not

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");

include("global.php");

$conn = new mysqli(server, dbuser, dbpw, db);

if( isset($_GET['user']) && $_GET['user']!='' ){
    $user = $_GET['user'];
}else{
    $user='';
}
//$customer = $_GET['customer'];
$querystring = "";
$querystring = "SELECT email, password, firstname, lastname from user ";
$result = $conn->query($querystring);

    enter code here

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"email":"'  . $rs["email"] . '",';
    $outp .= '"password":"'  . $rs["password"] . '",';
    $outp .= '"firstname":"'   . $rs["firstname"] . '",';   
    $outp .= '"lastname":"'   . $rs["lastname"]. '"}';
}
$outp .="]";

$conn->close();

echo($outp);
?>

Another way is that you can add @ in front of $_GET['user']

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");

include("global.php");

$conn = new mysqli(server, dbuser, dbpw, db);

@$user = $_GET['user'];

//$customer = $_GET['customer'];
$querystring = "";
$querystring = "SELECT email, password, firstname, lastname from user ";
$result = $conn->query($querystring);

    enter code here

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"email":"'  . $rs["email"] . '",';
    $outp .= '"password":"'  . $rs["password"] . '",';
    $outp .= '"firstname":"'   . $rs["firstname"] . '",';   
    $outp .= '"lastname":"'   . $rs["lastname"]. '"}';
}
$outp .="]";

$conn->close();

echo($outp);
?>
F5 Buddy
  • 474
  • 4
  • 4
-1

There are many ways to check if an array's index is undefined. The example below uses two methods: isset and empty.

The call to isset returns true if the array has the specified key.

The call to empty will return true if the value for the specified index is "empty" (null, empty string, zero, empty array, etc...) or if the specified key does not exist.

    <?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
    header("Content-Type: application/json; charset=UTF-8");

    include("global.php");

    $conn = new mysqli(server, dbuser, dbpw, db);

    $user = isset($_GET['user'])&&!empty($_GET['user']) ? $_GET['user'] : NULL;
    //$customer = $_GET['customer'];
    $querystring = "";
    $querystring = "SELECT email, password, firstname, lastname from user ";
    $result = $conn->query($querystring);



    $outp = "[";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
        if ($outp != "[") {$outp .= ",";}
        $outp .= '{"email":"'  . $rs["email"] . '",';
        $outp .= '"password":"'  . $rs["password"] . '",';
        $outp .= '"firstname":"'   . $rs["firstname"] . '",';   
        $outp .= '"lastname":"'   . $rs["lastname"]. '"}';
    }
    $outp .="]";

    $conn->close();

    echo($outp);
    ?>
Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
Howee
  • 1