可能重复:
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>
任何人都可以提供任何见解,为什么我收到这些通知......? 任何帮助将不胜感激。
答案 0 :(得分: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文档中查找它以了解更多信息。
答案 1 :(得分:-2)
而不是
$ Surname = $ _POST ['surname'];
待办事项
if(isset($_POST['surname']))
{
$Surname = $_POST['surname'];
}
或者
$Surname = (isset($_POST['surname'])) ? $_POST['surname'] : $some_string; /* or something */
您也可以使用extract()来定位所有这些后置变量。
答案 2 :(得分:-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;
}