I keep trying to fix this but it's giving me the same errors:
Notice: Undefined index: items in C:\xampp\htdocs\shop.php on line 9
Notice: Undefined index: body in C:\xampp\htdocs\shop.php on line 24
Here is my code:
<form action="" method="post">
<input type="radio" name="items" value="fed" /> Fed<br />
<input type="radio" name="items" value="body" /> Body<Br>
<input type="submit" name="submit"><br>
</form>
<?php
if(isset($_POST['submit'])) {
$items = $_POST['items'];
if($items=="fed") {
echo "You choose feds";
die();
}
else
echo "<Form action='' method='POST'>
<select name='body'>
<option value='head'>Head</option>
</select>
<input type='submit' name='submit' value='submit' /><br />
</form>";
if(isset($_POST['submit'])) {
$body = $_POST['body'];
$connect = mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("feds",$connect)or die(mysql_error());
$merchandise = mysql_query("SELECT * FROM merchandise WHERE body_part='$body' ")or die(mysql_error());
while($row = mysql_fetch_array($merchandise)) {
echo $row['item'];
echo "<bR>";
}
}
}
?>
Undefined index means that you are trying to access an array key that does not exist. Your form probably is not posting like you are expecting. A simple:
will show you what is posting.
You should make a hidden input for each form, instead af trying to listen to a submit button.. not all versions of php register the submit button as a value. Also you wrote the "if(isset($_POST['submit']))" twice, which will make php return both if-statements true.. hope this helps
The error messages say that you're accessing an item of an array that doesn't exist!
But in general you should check the existence of these variables (even if you're sure that the form was sent):
And please fix your SQL injection security hole!!
Please consider upgrading to MySQLi or PDO, too! There are many good resources on these topics out there.
These are not errors, they are notices. But they should be fixed in the production environment, as this gives you a hint that your code may not be working correctly in some cases.
Solution
Depending on what exactly you want to achieve, you can use the following code:
and you are sure your
$items
variable contains something (at least empty string). Of course depending on your code architecture, you can replace empty string withfalse
ornull
, but it is up to you.Tip about using
isset()
orempty()
Do not use them for checking if the specific element exists in array - use
array_key_exists()
. What is the difference?Let's assume
$my_array
looks like that:then:
empty()
on$my_array['b']
,$my_array['c']
and$my_array['d']
will returntrue
, as all these values are considered empty, and on$my_array['e']
will also returntrue
, as non-existent value/variable is also considered empty,isset()
on all elements will returntrue
with exception of$my_array['d']
, becauseisset()
treats variables equal tonull
as not set.isset($my_array['e'])
will also return `false (because variable is not set),array_key_exists()
will returntrue
for every key that actually exists within array, and will returnfalse
if it does not,So, as mentioned above, the better option for checking whether some element is defined in array is to use
array_key_exists()
, as it does not skip "empty" ornull
values.Appendix
There are links to the documentation:
isset()
- determines if a variable is set and is notNULL
,empty()
- determines whether a variable is empty (empty string,0
as integer,0.0
as float,'0'
- zero as string,null
,false
or empty arrayarray()
),array_key_exists()
- actually checks if the given key or index exists in the array,I am not able to understand the logic as to what you are trying to do. Can you explain a bit more? Also your first
if
condition is closed incorrectly. Your structure is as follows:So, check the brackets.
Try changing the code that echoes your form out to this:-
Notice I have added id's to the form elements, it is best to use both id and name attributes set to the same value for forms.