为什么我的PHP脚本总是在未定义的索引上出错


why my php script always error on undefined index

我在一个php文件中创建搜索函数。但在我分开它们之后,我的代码显示未定义的索引。任何可以帮助我解决这个错误的未定义索引的full_name不是字符串

<?php
if ($_REQUEST["string"]<>'') {
    $search_string = " AND (file_name LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["file_name"]<>''){
    $sql = "SELECT * FROM file WHERE file_name'".mysql_real_escape_string($_REQUEST["file_name"])."'".$search_string;
}
else {
    $sql = "SELECT * FROM file WHERE file_id>0 ".$search_string;
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $id=$row['file_id']?>
        <tr>
            <td><?php echo $row["file_id"]; ?></td>
            <td><?php echo $row["file_reference"]; ?></td>
            <td><?php echo $row["file_name"]; ?></td>
            <td><?php echo $row["file_location"]; ?></td>
            <td><?php echo $row["file_pdf"]; ?></td>
        </tr>
    <?php
    }
} else {
?>
<tr><td colspan="5">No results found.</td>
    <?php
    }
    ?>

有什么建议来解决我的代码中未定义的错误

使用isset检查是否存在变量或键

if (isset($_REQUEST['string']) && $_REQUEST["string"] != '') {
    // $_REQUEST['string'] exists and is non-empty
}

当您检查$_REQUEST["string"]<>''时,您实际上正在尝试访问该索引。使用isset()检查是否先设置

另外,您在哪一行上得到错误,因为它也可能是您试图访问不在查询响应中的列。这段代码中可能有很多地方会出现这个错误