PHP的未定义索引$id获取变量的值


undefined index of php $id get value of the variable

<?php
include 'connect.php';
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
    die("KO" . mysqli_connect_error());
}
$id = $_GET['id'];
$sql = "select * from tblabc where id = '$id'";
if (mysqli_num_rows($sql)==0){
    echo 'ok';
} else {
    $data = mysqli_fetch_assoc($sql);
}
?>

这是我编辑表记录的代码。但是当它执行时出现错误"notice: Undefined index: id"。如何解决这个问题。thx

<?php
include 'connect.php';
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
    die("KO" . mysqli_connect_error());
}
if(isset($_GET))
{
$id = $_GET['id'];
$sql = "select * from tblabc where id = '$id'";
if (mysqli_num_rows($sql)==0){
    echo 'ok';
} else {
    $data = mysqli_fetch_assoc($sql);
}
}
else
{
 echo 'nothing received';
}
?>

在使用get变量之前,使用上面的代码检查它的有效性。这样你就可以知道错误是从哪里来的

你应该试试这个

$id  = '';
if(isset($_GET['id']) && $_GET['id'] != '')
{
    $id = $_GET['id']; 
    $sql = "select * from tblabc where id = '$id'";
    if (mysqli_num_rows($sql)==0){
        echo 'ok';
    } else {
        $data = mysqli_fetch_assoc($sql);
    }
}
else
{
echo 'Id Not Defined';
}