未定义的索引 - PHP(之前可访问但现在不可访问)

时间:2016-05-23 02:40:03

标签: php

我正在尝试进入下一页,但它目前正在显示“通知:未定义的索引:你... y”。我之前访问的值是可访问的,但现在它显示此错误..我试图了解问题,但一切都是徒劳的..请帮助谢谢..

 <?php
$con = new mysqli("localhost","root","","mydb");



$titl = $_POST["u"];
$auth = $_POST["v"];
$isb = $_POST["w"];
$pub = $_POST["x"];
$keyw = $_POST["y"];

$q = "select * from mytable";   
$rs = $con->query($q);  

while($r = $rs->fetch_assoc())
{
    if($titl == $r["title"] && $auth == $r["author"] && $isb == $r["isbn"] && $pub == $r["name"] && $keyw == $r["keywords"])
    {
        echo"<html>
            <body>
            <style>
            body  
            {
                background-image: url('load1.gif');
                background-color: #cccccc;
            }
            </style>
            </body>
            </html>

            ";
            sleep(5);
             header("Location: info.php");
            exit;
    }

    else
    {
        if($check == 'notfound')
        {
        //  echo "Not Found...";
        }
    }
    echo"<br>";

  echo "</tr>";


}
echo "</table>";    
$con->close();
?>

1 个答案:

答案 0 :(得分:1)

这与您期望设置$_POST数据的方式有关。它未通过表单提交设置,或者您尝试直接访问此页面 - 这意味着您没有发布数据。

一般做法是,如果有$_POST数据集,则只运行这样的处理:

if(isset($_POST['submit'])) {
    $titl = filter_input(INPUT_POST, 'u');
    $auth = filter_input(INPUT_POST, 'v');
    $isb = filter_input(INPUT_POST, 'w');
    $pub = filter_input(INPUT_POST, 'x');
    $keyw = filter_input(INPUT_POST, 'y');

    //...rest of code
}

如您所见,我们已使用filter_input()来管理我们发送的POST数据。这是您的数据返回/设置的方式:

  • 如果设置了POST字段,则会返回。
  • 如果过滤器失败,您的变量将为false
  • 如果POST不存在,您的变量将为NULL

上述内容将基于以下形式:

<form action="your_page.php" method="POST">
    <input type="text" name="u">
    ...rest of the inputs...
    <input type="submit" name="submit" value="Submit">
</form>

请注意submit输入类型(name属性) - 以及每个具有name属性的输入。