我得到與下面的代碼以下錯誤:PHP未定義指數/變量
注意:未定義的變量:錯誤 C:\ XAMPP \ htdocs中\測試\項目\學習\ PHP \數據庫\形式和 數據庫\上線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中\測試\項目\學習\ PHP \數據庫\ Forms和 數據庫\項目。上行PHP 18
&
說明:未定義VARI能:錯誤 C:\ XAMPP \ htdocs中\測試\ PROJECTS \學習\ PHP \數據庫\ Forms和 數據庫\ project.php上線路名的35
所以很明顯的按鈕 「提交」 ISN」不管出於什麼原因都「看到」;我相信。這對我來說有點意義,如果我沒有弄錯: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和組合「等於」的if語句的條件,解決了身份不明的指標問題,因爲它涉及$ _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>';
}
?>
再次,這工作得很好,沒有錯誤。那麼,爲什麼我在發佈的第一個代碼塊中獲得了undefine變量錯誤?未定義的變量是由於隨後的if語句未能執行而導致的,那就是我假設一個與索引問題有關的問題,但是這些錯誤並沒有反映出來!
當我替換隻是點擊$條件==假的,如下:
$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;
}
}
我一起血腥代碼obviuosly執行成功獲得這三個不確定的索引錯誤,儘管三項指標被認爲undefined:
注意:未定義索引:第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行 您必須輸入一個狀態(0或1) !
**警告**:使用'mysqli'時,您應該使用參數化查詢和['bind_param'](http://php.net/manual/) en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值來實現此目的,因爲您將創建嚴重的[SQL注入漏洞](http://bobby-tables.com/)。如果這是在公共互聯網上,那麼您處於**嚴重**風險。 – tadman 2014-11-21 20:50:54
@tadman以前從未見過此通知 – 2014-11-21 20:54:30