[英]PHP undefined index / variable
使用以下代碼出現以下錯誤:
注意:未定義變量:第35行的C:\\ xampp \\ htdocs \\ test \\ projects \\ Learning \\ php \\ Databases \\ Forms and 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 and Databases \\ project.php中提交
和
注意:未定義變量:第35行的C:\\ xampp \\ htdocs \\ test \\ projects \\ Learning \\ php \\ Databases \\ Forms and Databases \\ project.php中的錯誤
顯然,無論出於何種原因,都不會“看到”名稱為“提交”的按鈕。 我相信。 對於我來說,這有點有意義……如果我沒有記錯的話:php以一種線性方式從頭到尾讀取,並且由於該形式在if語句之下,因此該索引尚不存在。 我認為,通過以下事實進一步證實了這一點:單擊“提交”按鈕后,所有錯誤都會消失,並因此執行if語句中的if語句的error(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;
}
}
...但是,在先前版本的代碼中,if語句條件的isset和“等於”的組合解決了與$ _POST ['submit']有關的Unidentified索引問題,這是該代碼: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>';
}
?>
再次,這工作正常,沒有錯誤。 那么,為什么在發布的第一段代碼中出現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;
}
}
盡管三個索引被認為是未定義的,但我仍然得到了成功執行的這三個未定義的索引錯誤以及笨拙的代碼:
注意:未定義的索引:第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您必須輸入一個url!
注意:第28行上的C:\\ xampp \\ htdocs \\ test \\ projects \\ Learning \\ php \\ Databases \\ Forms and Databases \\ project4.php中的狀態為undefined index:必須輸入狀態(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
一種解決方案是包裝可能不總是設置的變量以及表單或錯誤處理:
實際用法:
if (isset($_HaveNotSetThisVar)){
echo "Variable Set";
}
與數組相同:
if (isset($Array['NonExistantKey'])){
echo "Array Key Set";
}
這是錯誤處理的基礎,但是可以與其他功能堆疊在一起以進行更深入的處理。
聚苯乙烯
要確保的另一件事是,您使用的是正確的變量名,輸入錯誤也可能導致未定義的索引/變量:
$Vaar = "Test";
echo $Var; // Undefined Error
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.