首页 文章

PHP未定义的索引/变量

提问于
浏览
-1

我使用以下代码得到以下错误:

注意:未定义的变量:第35行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project.php中的错误

代码:

<?php 
    $clicked = false;
    if($clicked == false && isset($_POST['submit'])) {
        if ($_POST['label'] == '') {
            echo "<p>You must enter in a label!</p>";   
            $error = true;
        }
        if ($_POST['url'] == '') {
            echo "<p>You must enter in a url!</p>"; 
            $error = true;
        }
        if ($_POST['status'] == '') {
            echo "<p>You must enter in a status (0 or 1)!</p>"; 
            $error = true;  
        }
    }       
    if ($error != true) {
        if (isset($_POST['submit']) && $_POST['submit'] == '1'  ) {
            $query = "INSERT INTO nav (label, url, target, status, position) VALUES  ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])"; 
            $result = mysqli_query($dbc, $query);
            if ($result) {
                echo "<p>You've added a new navigation link!</p>";
            }
            else {
                echo "<p>An error has occured!</p>"; 
                echo mysqli_error($dbc);
                echo '<p>'.$query.'</p>';
            }
            $clicked = true;//submit button replaced        
        }
    }           
?>          

<h1>Navigation</h1> 
<h5>*Required Fields</h5>
<form action="project.php" method="post">
    <label>*Label:</label>
    <p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>         
    <label>Position:</label>
    <p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>           
    <label>*URL:</label>
    <p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>     
    <label>Target:</label>
    <p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>           
    <label>*Status:</label>
    <p><input type="text" name="status" size="10" value="" /></p>           

<?php 
    if ($clicked == false) {
        echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
    }
    else {
        echo '<p><a href "project.php"  id = resetBtn>Do Another</a></p>';
    }   
?>

当我做出以下更改时:

if($clicked == false && $_POST['submit'] == "1") {
    if ($_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
        $error = true;
    }
    if ($_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if ($_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}

我收到这些错误:

注意:未定义的索引:在第18行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project.php中提交

注意:未定义的变量:第35行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project.php中的错误

很明显,名称“提交”的按钮不会因任何原因被“看到”;我相信 . 这对我来说有点意义,有点......如果我没有弄错:php从头到尾以线性方式读取,并且因为表单在if语句之下,所以索引还不存在 . 我认为这进一步证实了这样一个事实:一旦我单击提交按钮,所有错误都会消失,因此执行if语句中的if语句的错误(echo)语句 .

这令人难以置信 . 这也不起作用......

if(isset($_POST['submit']) && $_POST['submit'] == '1') {
    if (isset($_POST['label']) && $_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
        $error = true;
    }
    if (isset($_POST['url']) && $_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if (isset($_POST['status']) && $_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}

...在以前版本的代码中,isset和"is equal to"的组合用于if语句's conditions, solved the Unidentified index problem, as it pertained to the $_POST[' submit '], here' s代码:ps:因为它属于这个特定的代码块,以下链接的伙伴tutorial尽管我和他做了完全相同的事情,但我正在跟随它,并没有出现任何这些错误 .

<?php 
    $clicked = false;
    if (isset($_POST['submit']) && $_POST['submit'] == '1'  ) {
        $query = "INSERT INTO nav (label, url, target, status, position) VALUES ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])";  
        $result = mysqli_query($dbc, $query);
        if ($result) {
            echo "<p>You've added a new navigation link!</p>";}
        else {
            echo "<p>An error has occured!</p>"; 
            echo mysqli_error($dbc);
            echo '<p>'.$query.'</p>';
        }       
        $clicked = true;//submit button replaced        
    }   
?>          

<h1>Navigation</h1> 
<h5>*Required Fields</h5>   
<form action="project2.php" method="post">
    <label>*Label:</label>
    <p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>
    <label>Position:</label>
    <p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>
    <label>*URL:</label>
    <p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>
    <label>Target:</label>
    <p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>
    <label>*Status:</label>
    <p><input type="text" name="status" size="10" value="" /></p>

<?php 
    if ($clicked == false) {
        echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
    }
    else {
        echo '<p><a href "project2.php"  id = resetBtn>Do Another</a></p>';
    }   
?>

再次,这工作得很好,没有错误 . 那么为什么我在我发布的第一个代码块中得到了undefine变量错误?未定义的变量是由于后续的if语句未能执行而导致的,这就是我假设与索引问题有关的问题,但是,错误并没有反映出来!

当我用$ clicked == false替换条件时,如下所示:

$clicked = false;
if($clicked == false) {
    if ($_POST['label'] == '') {
        echo "<p>You must enter in a label!</p>";   
         $error = true;
    }
    if ($_POST['url'] == '') {
        echo "<p>You must enter in a url!</p>"; 
        $error = true;
    }
    if ($_POST['status'] == '') {
        echo "<p>You must enter in a status (0 or 1)!</p>"; 
        $error = true;  
    }
}

我得到了这三个未定义的索引错误ALONG WITH BLOODY CODE,它显然成功执行,尽管三个索引被认为是未定义的:

注意:未定义的索引:第20行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project4.php中的标签您必须输入标签!

注意:未定义的索引:第24行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project4.php中的url你必须输入一个url!

注意:未定义的索引:第28行的C:\ xampp \ htdocs \ test \ projects \ Learning \ php \ Databases \ Forms和Databases \ project4.php中的状态您必须输入状态(0或1)!

2 回答

  • 0

    您需要在第一个IF语句之前定义$ error变量

    $error = true;
    

    因为PHP处理器对此一无所知

    同样在代码中没有关于定义的数据库连接,你需要定义$ dbc变量

    $dbc = mysqli_connect("localhost","my_user","my_password","my_db");
    
  • 2

    有很多示例代码,我一直没有看到,但是..长话短说,你的错误的解释如下:

    注意:未定义的变量

    当您尝试访问/使用它时,您的变量是未初始化的 .

    echo $_HaveNotSetThisVar; // as this is not set prior to using. You're getting an error
    

    注意:未定义的索引:

    很像你之前的错误 . 这次以数组形式:

    echo $Array['NonExistantKey']; // Undefined index
    

    解决这个问题的方法是包装变量,这些变量可能并不总是设置为表单或错误处理:

    http://php.net/isset


    实际用法:

    if (isset($_HaveNotSetThisVar)){
      echo "Variable Set";
    }
    

    和数组相同:

    if (isset($Array['NonExistantKey'])){
       echo "Array Key Set";
    }
    

    这是错误处理的基础,但可以与其他功能堆叠,以便进行更深入的处理 .

    P.S

    要确保的另一件事是,您使用的是正确的变量名称,未定义的索引/变量也可能由拼写错误引起:

    $Vaar = "Test";
     echo $Var; // Undefined Error
    

相关问题