PHP未定义索引/变量

PHP未定义索引/变量,php,mysql,forms,indexing,undefined,Php,Mysql,Forms,Indexing,Undefined,我使用以下代码得到以下错误: 注意:未定义变量:中的错误 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms和 第35行的Databases\project.php 守则: <?php $clicked = false; if($clicked == false && isset($_POST['submit'])) { if ($_POST['label'] == '')

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

注意:未定义变量:中的错误 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms和 第35行的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>';
    }   
?>

这里有很多示例代码,我还没有完全看过,但是。。长话短说,您的错误解释如下:

注意:未定义的变量

当您试图访问/使用变量时,该变量未初始化

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

注意:未定义索引:

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

echo $Array['NonExistantKey']; // Undefined index 
解决此问题的一个方法是包装变量,这些变量可能并不总是设置为表单或错误处理:


实际用途:

if (isset($_HaveNotSetThisVar)){
  echo "Variable Set";
}
阵列也是如此:

if (isset($Array['NonExistantKey'])){
   echo "Array Key Set";
}
这是错误处理的基础,但可以与其他函数叠加以进行更深入的处理

p.S

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

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

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

$error = true;
因为PHP处理器对此一无所知

此外,在代码中没有定义任何数据库连接,您需要定义$dbc变量

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

警告:使用
mysqli
时,应使用参数化查询,并将用户数据添加到查询中。不要使用字符串插值来完成此操作,因为这样会创建严重的错误。如果这是在公共互联网上,您将面临严重的风险。@tadman从未见过此通知previously@theconfusedbroom嗯,这是你的密码,你改变了什么?非常感谢。我该如何表扬你?我点击了复选标记,希望它成功了。我是个笨蛋,但该死的,我不敢相信我居然错过了那个LOL。至于dbc,谢谢你,我知道。
if (isset($Array['NonExistantKey'])){
   echo "Array Key Set";
}
 $Vaar = "Test";
 echo $Var; // Undefined Error
$error = true;
$dbc = mysqli_connect("localhost","my_user","my_password","my_db");