2012-11-20 40 views
-1

可能重複:
PHP: 「Notice: Undefined variable」 and 「Notice: Undefined index」每個部分都有未定義的索引?

我已經大約三個工作日花在這個問題,不知道如何解決它我得到...

Notice: Undefined index: surname in C:\xampp\htdocs\Locus.php on line 25 
Notice: Undefined index: prison in C:\xampp\htdocs\Locus.php on line 26 
Notice: Undefined index: NI in C:\xampp\htdocs\Locus.php on line 27 
Notice: Undefined index: Q1 in C:\xampp\htdocs\Locus.php on line 28 
Notice: Undefined index: Q2 in C:\xampp\htdocs\Locus.php on line 29 
Notice: Undefined index: Q3 in C:\xampp\htdocs\Locus.php on line 30 
Notice: Undefined index: Q4 in C:\xampp\htdocs\Locus.php on line 31 
Notice: Undefined index: Q5 in C:\xampp\htdocs\Locus.php on line 32 

....等

這裏是我的HTML ...

<!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> 
<script type="text/javascript" src="Locus.js"></script> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Locus of control test</title> 
<link href="Locus.css" rel="stylesheet" type="text/css" /> 

</head> 

<body> 

<form action="Locus.php" id="Locus" method="post" name="Locus" onsubmit="return validateForm()" 

<h4>Surname: <input type="text" name="surname"/> <br/>Prison: <input type="text" name="prison"/><br/>National Insurance: <input type="text" name="NI"/></h4> 
<h3> Please carefully read all the instructions given on the left hand panel.</h3> 
<h1 class="Header"> LOCUS OF CONTROL</h1> 
<h2>Please click submit ONLY when all questions have been completed <br/> <input name="submit" id="submit" type="submit" value="Check & Submit" /></h2> 

<p>1. I can anticipate difficulties and take action to avoid them. <br /> 
<input type="radio" name="Q1" value="4" /> Always Agree <input type="radio" name="Q1" value="3" /> Agree <input type="radio" name="Q1" value="2" /> Unsure <input type="radio" name="Q1" value="1" /> Disgree <input type="radio" name="Q1" value="0" /> Always Disagree 
</p> 


<p>2. A great deal of what happens to me is just a matter of chance. <br /> 
<input type="radio" name="Q2" value="0" /> Always Agree <input type="radio" name="Q2" value="1" /> Agree <input type="radio" name="Q2" value="2" /> Unsure <input type="radio" name="Q2" value="3" /> Disgree <input type="radio" name="Q2" value="4" /> Always Disagree 
</p> 

<p>3. Everyone knows that luck or chance determines the future. <br /> 
<input type="radio" name="Q3" value="0" /> Always Agree <input type="radio" name="Q3" value="1" /> Agree <input type="radio" name="Q3" value="2" /> Unsure <input type="radio" name="Q3" value="3" /> Disgree <input type="radio" name="Q3" value="4" /> Always Disagree 
</p> 

<p>4. I can control my problems only if I have outside support. <br /> 
<input type="radio" name="Q4" value="0" /> Always Agree <input type="radio" name="Q4" value="1" /> Agree <input type="radio" name="Q4" value="2" /> Unsure <input type="radio" name="Q4" value="3" /> Disgree <input type="radio" name="Q4" value="4" /> Always Disagree 
</p> 

<p>5. When I make plans I am almost certain I can make them work. <br /> 
<input type="radio" name="Q5" value="4" /> Always Agree <input type="radio" name="Q5" value="3" /> Agree <input type="radio" name="Q5" value="2" /> Unsure <input type="radio" name="Q5" value="1" /> Disgree <input type="radio" name="Q5" value="0" /> Always Disagree 
</p> 

<p>6. My problems will dominate all my life. <br /> 
<input type="radio" name="Q6" value="0" /> Always Agree <input type="radio" name="Q6" value="1" /> Agree <input type="radio" name="Q6" value="2" /> Unsure <input type="radio" name="Q6" value="3" /> Disgree <input type="radio" name="Q6" value="4" /> Always Disagree 
</p> 

<p>7. My mistakes and problems are my responsibility to deal with. <br /> 
<input type="radio" name="Q7" value="4" /> Always Agree <input type="radio" name="Q7" value="3" /> Agree <input type="radio" name="Q7" value="2" /> Unsure <input type="radio" name="Q7" value="1" /> Disgree <input type="radio" name="Q7" value="0" /> Always Disagree 
</p> 

<p>8. Becoming a success is a matter of hard work, luck has little or nothing to do with it. <br /> 
<input type="radio" name="Q8" value="4" /> Always Agree <input type="radio" name="Q8" value="3" /> Agree <input type="radio" name="Q8" value="2" /> Unsure <input type="radio" name="Q8" value="1" /> Disgree <input type="radio" name="Q8" value="0" /> Always Disagree 
</p> 

<p>9. My life is controlled by outside actions and events. <br /> 
<input type="radio" name="Q9" value="0" /> Always Agree <input type="radio" name="Q9" value="1" /> Agree <input type="radio" name="Q9" value="2" /> Unsure <input type="radio" name="Q9" value="3" /> Disgree <input type="radio" name="Q9" value="4" /> Always Disagree 
</p> 

<p>10. I believe people are victims of circumstances beyond their control. <br /> 
<input type="radio" name="Q10" value="0" /> Always Agree <input type="radio" name="Q10" value="1" /> Agree <input type="radio" name="Q10" value="2" /> Unsure <input type="radio" name="Q10" value="3" /> Disgree <input type="radio" name="Q10" value="4" /> Always Disagree 
</p> 

<p>11. To continually manage my problems I need professional help. <br /> 
<input type="radio" name="Q11" value="0" /> Always Agree <input type="radio" name="Q11" value="1" /> Agree <input type="radio" name="Q11" value="2" /> Unsure <input type="radio" name="Q11" value="3" /> Disgree <input type="radio" name="Q11" value="4" /> Always Disagree 
</p> 

<p>12. When I am under stress the tightness in my muscles is due to things outside my control.<br /> 
<input type="radio" name="Q12" value="0" /> Always Agree <input type="radio" name="Q12" value="1" /> Agree <input type="radio" name="Q12" value="2" /> Unsure <input type="radio" name="Q12" value="3" /> Disgree <input type="radio" name="Q12" value="4" /> Always Disagree 
</p> 

<p>13. 1 believe a person really can be master of his own fate.<br /> 
<input type="radio" name="Q13" value="4" /> Always Agree <input type="radio" name="Q13" value="3" /> Agree <input type="radio" name="Q13" value="2" /> Unsure <input type="radio" name="Q13" value="1" /> Disgree <input type="radio" name="Q13" value="0" /> Always Disagree 
</p> 

<p>14. It is impossible to control irregular fast breathing when I am having difficulties.<br /> 
<input type="radio" name="Q14" value="0" /> Always Agree <input type="radio" name="Q14" value="1" /> Agree <input type="radio" name="Q14" value="2" /> Unsure <input type="radio" name="Q14" value="3" /> Disgree <input type="radio" name="Q14" value="4" /> Always Disagree 
</p> 

<p>15. I understand why my problems vary so much from one occasion to another. <br /> 
<input type="radio" name="Q15" value="4" /> Always Agree <input type="radio" name="Q15" value="3" /> Agree <input type="radio" name="Q15" value="2" /> Unsure <input type="radio" name="Q15" value="1" /> Disgree <input type="radio" name="Q15" value="0" /> Always Disagree 
</p> 

<p>16. I am confident of being able to deal successfully with future problems.<br /> 
<input type="radio" name="Q16" value="4" /> Always Agree <input type="radio" name="Q16" value="3" /> Agree <input type="radio" name="Q16" value="2" /> Unsure <input type="radio" name="Q16" value="1" /> Disgree <input type="radio" name="Q16" value="0" /> Always Disagree 
</p> 

<p>17. In my case maintaining control over my problems is mainly due to luck. <br /> 
<input type="radio" name="Q17" value="0" /> Always Agree <input type="radio" name="Q17" value="1" /> Agree <input type="radio" name="Q17" value="2" /> Unsure <input type="radio" name="Q17" value="3" /> Disgree <input type="radio" name="Q17" value="4" /> Always Disagree 
</p> 

<p>18. I have often been blamed for events beyond my control. <br /> 
<input type="radio" name="Q18" value="0" /> Always Agree <input type="radio" name="Q18" value="1" /> Agree <input type="radio" name="Q18" value="2" /> Unsure <input type="radio" name="Q18" value="3" /> Disgree <input type="radio" name="Q18" value="4" /> Always Disagree 
</p> 
<h5>Please click 'submit' at the start of this test</h5> 
</form> 
</body> 
</html> 

,這裏是我的PHP ....

<!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" /> 
<link href="Locus.css" rel="stylesheet" type="text/css" /> 
<title>Locus Test Complete</title> 
</head> 

<body> 
<?php 
/*variable declaration*/ 
$username="root"; 
$password=""; 
$database_server="localhost"; 

/*server connection*/ 
$database="Locus"; 
mysql_connect($database_server,$username,$password) or die("cannot connect"); 

/*database connection*/ 
@mysql_select_db($database) or die("Unable to select database"); 


/*create values from Locus to PHP variables*/ 
$Surname = $_POST['surname']; 
$Prison = $_POST['prison']; 
$NI = $_POST['NI']; 
$Q1 = $_POST['Q1']; 
$Q2 = $_POST['Q2']; 
$Q3 = $_POST['Q3']; 
$Q4 = $_POST['Q4']; 
$Q5 = $_POST['Q5']; 
$Q6 = $_POST['Q6']; 
$Q7 = $_POST['Q7']; 
$Q8 = $_POST['Q8']; 
$Q9 = $_POST['Q9']; 
$Q10 = $_POST['Q10']; 
$Q11 = $_POST['Q11']; 
$Q12 = $_POST['Q12']; 
$Q13 = $_POST['Q13']; 
$Q14 = $_POST['Q14']; 
$Q15 = $_POST['Q15']; 
$Q16 = $_POST['Q16']; 
$Q17 = $_POST['Q17']; 
$Q18 = $_POST['Q18']; 


/*assign the values to the locus of control scores table*/ 
$query = "INSERT INTO locus of control scores VALUES ('','$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')"; 
mysql_query($query); 


/*create query*/ 
mysql_query($query); 

/*close server connection*/ 
mysql_close(); 

?> 
</body> 
</html> 

任何人都可以提供任何見解,爲什麼我得到這些通知...? 任何幫助將不勝感激。

+1

因爲沒有人在發佈這些變量?您不能在沒有看到該通知的情況下訪問PHP中不存在的數組鍵。在使用它們進行分配之前,您需要有條件地檢查它們的存在。 –

+1

您的開放標籤缺少一個右括號:'

' –

+0

@MikeB not一個確切的副本,但一個可能的,我會說;) –

回答

1

您不能引用陣列中沒有它們的鍵,這就是您想要從$_POST陣列中執行的操作,因爲請求沒有發佈這些值。

爲了擺脫通知,更改所有聲明你來自哪裏,$_POST從拉動值直接的:

$Surname = $_POST['surname']; 

要:

$Surname = isset($_POST['surname']) ? $_POST['surname'] : ""; // Some default value 

而且,在你的代碼中要插入兩次看起來相同的查詢。而且,VDP帶來了一個很好的觀點,你不想插入空行,所以要解決這個問題,你可以用if語句包裝整個邏輯來插入,檢查$_POST是否爲空,或者檢查每個變量是否有插入前有效的值。

編輯:好吧,更徹底的解釋。

當您嘗試訪問陣列中沒有的陣列中的鍵時,您會收到此通知。所以,你的$_POST數組可能是空的,因爲當你訪問頁面時,你沒有提交POST請求,或者如果你這樣做,你沒有發送它期望的變量。

所以$_POST樣子:

$_POST = array(); 

而你要做的:

$Surname = $_POST['surname']; // DOESN'T EXIST! 

顯然,你不能這樣做。因此,而不是直接從$_POST抓住的東西,你是否該鍵的$_POST數組中確實存在:

$Surname = isset($_POST['surname']) ? $_POST['surname'] : ""; 

然而,這僅僅是速記:

if(isset($_POST['surname'])) { 
    $Surname = $_POST['surname']; 
} else { 
    $Surname = ""; 
} 

這就是所謂的三元運算符,你可以在PHP文檔中查閱更多關於它的信息。

+0

好的,這工作,但我不明白爲什麼,謝謝。 –

+1

應該使用另一種方法,因爲在他的例子中,你會插入空記錄 – VDP

+0

@VDP - 好點,我已經更新了我的答案。另外,Arr,我解釋了爲什麼它在第一句中起作用... – nickb

-2

代替

$姓= $ _POST [ '姓'];

if(isset($_POST['surname'])) 
{ 
    $Surname = $_POST['surname']; 
} 

或者

$Surname = (isset($_POST['surname'])) ? $_POST['surname'] : $some_string; /* or something */ 

你也可以只使用提取物()的本地化所有這些崗位的變量。

-2

如果要徹底確保這些聲明不會搞砸你的應用程序,並希望速戰速決使用

error_reporting(0); 

或使用@$variable;

我再說一遍,這是不是一個真正的解決方案,但暫時的快速修復。

對於真正修復它,你應該總是檢查變量的存在。

echo $myvariable; // will output notice 

$myvariable = null; 

echo $myvariable; // will output nothing but nor the notice 

的一個好方法是使用:

if(isset($_POST['myvariable'])){ 
$myvariable = $_POST['myvariable']; 
} else { 
$myvariable = null; 
}