如何修复未定义的索引$ user = $ _GET ['user'];

时间:2018-11-20 05:50:07

标签: php

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

这是数据库中的PHP代码,需要使用get方法。错误显示未定义的索引:$user = $_GET['user'];在运行时显示此错误。如何解决?

3 个答案:

答案 0 :(得分:0)

我在下面的代码中添加了IF条件,您可以检查它。通过添加此条件,您可以解决未定义索引问题

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

答案 1 :(得分:0)

您可以通过在$ _GET ['user']中添加isset并检查其是否为空来解决此问题

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

另一种方法是,您可以在$ _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);
?>

答案 2 :(得分:-1)

有很多方法可以检查数组的索引是否未定义。下面的示例使用两种方法:issetempty

如果数组具有指定的键,则对isset的调用将返回true。

如果指定索引的值为“空”(空,空字符串,零,空数组等),或者指定的键不存在,则对empty的调用将返回true。

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