簡體   English   中英

php中未定義的索引和變量

[英]Undefined indices and variables in php

這是一本教科書問題,其中我遵循了確切的編碼。 然而,我不斷得到未定義的索引和未定義的vairables的錯誤。 我繼續檢查我的代碼,我認為我錯過了疲勞的錯誤。 這是代碼。 有什么建議么。 我逆流而上。 這是與此相關htm文件

以下是錯誤消息:

 Undefined index: firstname in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 10   PHP Notice: Undefined index: lastname in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on   line 11 PHP Notice: Undefined index: whenithappened in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 12 PHP Notice: Undefined index: howlong in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 13 PHP Notice: Undefined index: howmany in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 14 PHP Notice: Undefined index: aliendescription in  D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 15 PHP Notice: Undefined index: whattheydid in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 16 PHP Notice: Undefined index: fangspotted in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 17 PHP Notice: Undefined index: email in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 18 PHP Notice: Undefined index: other in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 19 PHP Notice: Undefined variable: name in D:\Inetpub\Ciswebs\CIS54\TYSON_SCHWEIDEL\cReport.php on line 33 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Alien Abduction2</title>
 </head>

 <body>
 <?php
 $first_name = $_POST['firstname'];
 $last_name = $_POST['lastname'];
 $when_it_happened = $_POST['whenithappened'];
 $how_long = $_POST['howlong'];
 $how_many = $_POST['howmany'];
 $alien_description = $_POST['aliendescription'];
 $what_they_did = $_POST['whattheydid'];
 $fang_spotted = $_POST['fangspotted'];
 $email = $_POST['email'];
 $other = $_POST['other'];

 $dbc = mysqli_connect('localhost','cis54student','student','cis54')
 or die('Error connecting to MySQL server');
 $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

 $result = mysqli_query($dbc, $query)
 or die('Error querying database.' . mysqul_error());

 mysqli_close($dbc);

 echo "Thanks for submitting the form $name<br />";
 echo "You were abducted '  $when_it_happened<br />";
 echo "And were gone for ' . $how_long <br />";
 echo "Number of aliens: ' . $how_many <br />";
 echo "Describe them: ' . $alien_description <br />";
 echo "The aliens did this:  $what_they_did <br />";
 echo "Was Fang there?  $fang_spotted <br />";
 echo "Other comments: ' . $other <br />";
 echo 'Your email address is ' . $email;


?>
</body>
</html>

單獨改變這一點

<input id="fangspotted" 
       name="fangspotted" 
       type="radio" 
       value="yes" 
       checked="checked" />

沒有警告:)

我相信你收到通知是因為當你加載頁面時(當它沒有提交時,即只是點擊這里 ),這些變量沒有定義。 你有兩個解決方案。

  1. 檢查$_POST是否存在提交按鈕,然后采取相應措施
  2. 在使用之前,更改所有變量以使用isset()測試$_POST數組。

解決方案#1:

if( isset( $_POST['submit']))
{
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $when_it_happened = $_POST['whenithappened'];
    ....
}

解決方案#2:

$first_name = isset( $_POST['firstname']) ? $_POST['firstname'] : '';
$last_name = isset( $_POST['lastname']) ? $_POST['lastname'] : '';
...

另外,正如馬里奧指出的那樣,你將mysql_error拼錯為mysqul_error

看看你提交的地方。 我將form action =“report.php”更改為creport.php,它解決了很大一部分問題。 行中還有bug

echo "Thanks for submitting the form $name<br />";

因為$ name永遠不會定義。

我可以建議一些改進

首先,我建議查看http://php.net/manual/en/function.extract.php 它將節省大量代碼來提取$ _POST。

添加isset測試以確保在使用變量之前設置變量

在這種情況下你可以使用。

echo (isset($var)?$var:'');

如前所述,您的代碼確實存在一些安全漏洞,並且應該在服務器端執行額外級別的錯誤檢查(如果禁用了javascript),並且內聯javascript不被視為良好實踐(為表單提交添加事件監聽器)。 你的老師可能還沒有涵蓋這些(或者可能甚至不知道更好)。

你真正需要的是檢查是否有POST請求。

上述答案的解決方案#2沒什么意義。
它的唯一目的是關閉錯誤信息。
雖然只是在沒有任何處理的情況下嘔吐錯誤,但是只會讓你的代碼變得混亂。

所以,你必須檢查是否有帖子請求。

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //here goes all your code
}

你會看到沒有錯誤消息。

真正需要的唯一情況是復選框類型。 始終發送所有其他類型,因此無需檢查它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM