-1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I have spent around three days on this problem and have no idea how to fix it i'm getting...

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

....etc

here is my 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>

and here is my 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>

Can anyone offer any insight as to why i'm getting these notices...? any help would be greatly appreciated.

Community
  • 1
  • 1
  • 1
    Because noone is posting those variables? You can't access array keys in PHP that don't exist without seeing that notice. You need to conditionally check for their existence before using them for assignment. – Mike B Nov 20 '12 at 14:03
  • 1
    your opening form tag is missing a closing bracket: `
    `
    – Zathrus Writer Nov 20 '12 at 14:04
  • @MikeB not an exact duplicate, but a possible one, I'd say ;) – Zathrus Writer Nov 20 '12 at 14:05
  • so how would I fix this? – Arr Connor Nov 20 '12 at 14:06
  • @ZathrusWriter How do you figure? The title and bulk of the question addresses the PHP errors. – Mike B Nov 20 '12 at 14:06
  • Check your input. `if(isset($_POST['surname'])){ ... ` – VDP Nov 20 '12 at 14:07
  • @MikeB well, I'd say an exact duplicate would be the very same question posted twice, whereas a possible duplicate is a question with the same idea behind it – Zathrus Writer Nov 20 '12 at 14:07
  • 1
    @ZathrusWriter If it were the same question posted twice I wouldn't have voted to close, I would have flagged it because the answer here wouldn't have added anything of value to the [almost 5,000 other similar questions](http://stackoverflow.com/search?q=%5Bphp%5D+Notice%3A+Undefined+index). – Mike B Nov 20 '12 at 14:08

3 Answers3

1

You can't reference keys from an array that doesn't have them, and that's what you're trying to do from the $_POST array, since the request didn't have those values posted.

To get rid of the notices, change all of your declarations where you pull values from $_POST directly from:

$Surname = $_POST['surname'];

To:

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

Also, in your code you're inserting the same query twice it looks like. And, VDP brings up a good point, you don't want to insert empty rows, so to fix that, you can wrap the whole logic for inserting with an if statement, check if the $_POST is empty, or check if every variable has a valid value before inserting.

Edit: OK, a more thorough explanation.

When you try to access keys from an array that the array does not have, you get this notice. So, your $_POST array is likely empty, because when you accessed the page, you didn't submit a POST request to it, or if you did, you didn't send the variables it's expecting.

So $_POST looks like:

$_POST = array( );

And you're trying to do:

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

Clearly, you can't do that. So, instead of directly grabbing things from $_POST, you check if that key actually exists in the $_POST array:

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

However, this is just shorthand for:

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

It's called the ternary operator, and you can look it up on the PHP documentation to read more about it.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • ok, that worked, but I don't understand why, thank you. – Arr Connor Nov 20 '12 at 14:10
  • 1
    should use another approach because in his example you will insert empty records – VDP Nov 20 '12 at 14:10
  • @VDP - Good point, I've updated my answer. Also, Arr, I explained why it worked in the first sentence... – nickb Nov 20 '12 at 14:12
  • @nickb I like the last line.. 'check if every variable has a valid value before inserting.' He should escape his input values too!! – VDP Nov 20 '12 at 14:14
  • Well this *could* be just a shell of the actual code - It would make sense to properly validate all of his input, including escaping the strings coming in. Good point! – nickb Nov 20 '12 at 14:15
  • I'm sorry I just don't understand PHP that well to wrap everything inside an if statement, i still don't understand how the statement you gave me works or even if it will add the radio values. – Arr Connor Nov 20 '12 at 14:20
  • @Arr - I've updated my answer with tons of detail, there should be no doubt now. – nickb Nov 20 '12 at 14:27
  • thank you, i'll research this. It's all good being given the right code for the job but if i don't understand what's happening then it's not really helping, thanks for your time and explanation. – Arr Connor Nov 20 '12 at 14:31
  • one last question with the "" at the end of $Q1 = isset( $_POST['Q1']) ? $_POST['Q1'] : ""; would this be where I could place a message such as $Q1 = isset( $_POST['Q1']) ? $_POST['Q1'] : "Qestion 1 is incomplete!"; ? – Arr Connor Nov 20 '12 at 14:53
  • i don't think this works, asking it to return a variable through echo doesn't do anything, all i seem to have done is remove the eror messages, the page still doesnt work, and i understand even less as to why. – Arr Connor Nov 20 '12 at 15:30
-2

Instead of

$Surname = $_POST['surname'];

Do

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

Or

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

You could also just use extract() for localising all those post variables.

Toby
  • 957
  • 2
  • 14
  • 34
-2

If you are COMPLETELY SURE that those notices won't screw your application and want a quick fix use

error_reporting(0);

or use @$variable;

I repeat, this is not really a solution but a temporal quick fix.

For really fixing it you should always check for the existence of the variables.

echo $myvariable; // will output notice

$myvariable = null;

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

A good way is to use :

if(isset($_POST['myvariable'])){
$myvariable = $_POST['myvariable'];
} else {
$myvariable = null;
}
aleation
  • 4,796
  • 1
  • 21
  • 35