我在下面的代码中收到以下错误:
注意:未定义的变量:错误 C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and 数据库''项目.php在第 35 行
代码:
<?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;
}
}
我收到这些错误:
注意:未定义的索引:提交于 C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and 数据库''项目.php在第 18 行
&
注意:未定义的变量:错误 C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and 数据库''项目.php在第 35 行
很明显,无论出于何种原因,名称"提交"的按钮都没有被"看到";我相信。 这对我来说有些道理,有点...如果我没记错的话: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 等于" 对于 if 语句的条件的组合,解决了 Unidentity 索引问题,因为它与 $_POST['submit'] 有关,这是该代码:ps:由于它与这个特定的代码块有关,尽管我和他做了完全相同的事情,但我正在关注以下链接教程中的那个家伙没有出现任何这些错误。
<?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>';
}
?>
同样,这工作得很好,没有错误。那么,为什么我在发布的第一个代码块中出现未定义变量错误呢?未定义的变量是由于后续的 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;
}
}
我得到这三个未定义的索引错误以及血腥代码,尽管这三个索引被认为是未定义的,但它显然成功执行:
注意:未定义的索引:C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and Databases''project4.php 第 20 行中的标签您必须输入标签!
注意:未定义的索引:C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and Databases''project4.php 第 24 行中的 url您必须输入一个网址!
注意:未定义的索引:C:''xampp''htdocs''test''projects''Learning''php''Databases''Forms and Databases''project4.php 第 28 行中的状态您必须输入状态(0 或 1)!
你需要在第一个 IF 语句之前定义$error变量
$error = true;
因为PHP处理器对此一无所知
同样在代码中没有关于定义的数据库连接的内容,您需要定义$dbc变量
$dbc = mysqli_connect("localhost","my_user","my_password","my_db");
有很多示例代码,我没有从头到尾看过,但是..长话短说,对您的错误的解释如下:
注意:未定义的变量
当您尝试访问/使用它时,您的变量是未初始化的。
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";
}
这是错误处理的基础,但可以与其他函数堆叠在一起以进行更深入的处理。
附言
要确保的另一件事是,您使用的是正确的变量名称,未定义的索引/变量也可能由拼写错误引起:
$Vaar = "Test";
echo $Var; // Undefined Error