【发布时间】:2015-01-20 02:40:02
【问题描述】:
以下代码出现以下错误:
注意:未定义的变量:错误 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms 和 Databases\project.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 和 Databases\project.php 第 18 行
&
注意:未定义的变量:错误 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms 和 Databases\project.php 第 35 行
很明显,名称“提交”的按钮无论出于何种原因都没有被“看到”;我相信。这对我来说有点道理……如果我没记错的话:php 以线性方式从头到尾读取,并且由于表单位于 if 语句下方,因此索引尚不存在。我认为进一步证实了这一点,即一旦我单击提交按钮,所有错误都会消失,并且 if 语句中的 if 语句的错误(回显)语句因此被执行。
这令人难以置信。这也不起作用...
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;
}
}
...然而,在以前版本的代码中,if 语句条件的 isset 和“等于”的组合解决了与 $_POST['submit' 相关的未识别索引问题],代码如下: 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>';
}
?>
再次,这工作得很好,没有错误。那么,为什么我在发布的第一个代码块中会出现未定义变量错误?未定义的变量是由于后续的 if 语句未能执行而出现的,这就是我假设与索引问题有关的问题,但是,错误并没有反映这一点!
当我将条件替换为just $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;
}
}
我得到这三个未定义的索引错误以及显然成功执行的血腥代码,尽管三个索引被认为是未定义的:
注意:未定义索引:第 20 行 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php 中的标签 您必须输入标签!
注意:未定义索引:第 24 行 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php 中的 url 你必须输入一个网址!
注意:未定义索引:第 28 行 C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php 中的状态 您必须输入状态(0 或 1)!
【问题讨论】:
-
警告:当使用
mysqli时,您应该使用参数化查询和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值来完成此操作,因为您将创建严重的SQL injection bugs。如果这是在公共互联网上,您将面临严重风险。 -
@tadman 以前从未见过此通知
标签: php mysql forms indexing undefined