2011-11-18 27 views
0

這是一個教科書問題,其中我遵循了確切的編碼。然而我一直在收集未定義指數和未定義可變因素的錯誤。我繼續研究我的代碼,我想我錯過了疲勞的錯誤。這是代碼。有什麼建議麼。我反對時鐘。這裏是htm file associated with this在php中未定義的索引和變量

以下是錯誤消息:

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> 
+0

當心SQL注入的,你的代碼是脆弱的。 –

+0

確切的錯誤信息會有幫助 – Chris

+0

這些是調試通知。不調試時關閉它們。那裏確實存在更大的問題。把這本教科書扔掉,如果它是一個例子。還有另一個錯字:'PHP致命錯誤:調用未定義的函數mysqul_error()' – mario

回答

1

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

  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_errormysqul_error

+1

這會使通知消失,但不會將丟失的字段帶回。 – mario

+0

這只是我們文章中的第2章,所以還有很多我還不明白。我會用你的建議,因爲我需要幫助。我們還沒有開始使用isset。我不知道這將如何與教練飛行。但我會用它。 – swydell

+1

隱藏錯誤並不能解決根本問題。我很少忽視通知,因爲它們指出未來可能出現的問題。 –

0

看看你要提交的地方。我將表單action =「report.php」更改爲creport.php,並解決了它的一大塊問題。也有行中的錯誤

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並不是好的做法和inline(添加事件監聽器表單提交)。你的老師可能還沒有涉及這些(或者可能甚至可能不知道更好)。

0

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

從上面的答案解決方案#2沒有什麼意義。
只有它的目的是關閉錯誤消息。
雖然只是在沒有任何處理的情況下發生堵塞錯誤並不會帶來好處,但會讓代碼變得混亂。

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

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

而且您將看不到任何錯誤消息。

確實需要isset檢查的唯一情況是複選框類型。 所有其他類型始終被髮送,所以沒有必要檢查它們。

+0

你**總是**必須檢查什麼被髮送到您的腳本...除非我誤解了你的最後兩句話? – nickb

+0

檢查沒有目的,只是爲了檢查沒有意義。如果您認爲您必須檢查水手,請在發生失誤時採取措施。如果你什麼都不做 - 你的支票是無用的 –

1

變化僅此

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

沒有警告:)