oraya Posted January 15, 2008 Share Posted January 15, 2008 Can anyone see why i'm getting undefined index errors? This is driving me loopy <?php include_once '../inc/config.php'; include_once 'hearse.php'; $id=mysql_real_escape_string($_POST['id']); $cat=mysql_real_escape_string($_POST['cat']); $title=mysql_real_escape_string($_POST['title']); $info=mysql_real_escape_string($_POST['info']); $price=mysql_real_escape_string($_POST['price']); $query = "INSERT INTO eco_friendly VALUES ('$id', '$cat', '$title', '$info', '$price')"; mysql_query($query); header("Location: added.php"); mysql_close(); ?> I've gone over everything, all is correct and as it should be but it's just throwing a hissy fit. Sigh this is driving me mad slowly. If anyone can see anything that they think might be the reason, could you let me know. I've checked form, and database, I haven't miss spelled any fields. I just don't understand it. I have other input forms that are identical and they work just fine. Many thanks in advance, Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2008 Share Posted January 15, 2008 You would need to post the exact error message to get the quickest help. Posting the form would help as well. Your form is likely not setting a field, possibly because a tag in the form is broken or located outside the form tags. Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439375 Share on other sites More sharing options...
oraya Posted January 15, 2008 Author Share Posted January 15, 2008 Here it is: Notice: Undefined index: id in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 5 Notice: Undefined index: cat in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 6 Notice: Undefined index: title in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 7 Notice: Undefined index: info in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 8 Notice: Undefined index: price in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php:5) in c:\JUST PC PATH ADDRESS\eco_friendly\post_add.php on line 14 I've checked every thing and can't find any thing a miss. Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439381 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2008 Share Posted January 15, 2008 Since they are all undefined, that would indicate that either the code is being executed unconditionally (not as the result of the form being submitted) or the form is not setting any $_POST variables. Still waiting for your form code to be posted to provide the quickest solution. There are a number of possible reasons and seeing actual code is the quickest way to find a solution. Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439387 Share on other sites More sharing options...
oraya Posted January 15, 2008 Author Share Posted January 15, 2008 <form action="post_add.php" method="post"> <input type="hidden" name="id" /> Category: <select name="cat"> <option value="">Select Category</option> <option value=""></option> <option value="Coffin">Coffin</option> <option value="Casket">Casket</option> <option value="Urn">Urn</option> </select> Title: <input type="text" size="60" name="title" /> Description: <textarea name="info"></textarea> Price: <input type="text" size="10" maxlength="10" name="price" /> <input type="button" value="Cancel Add" onClick="history.go(-1)" /> <input type="submit" value="Add New Eco Product" /> </form> Sorry yes I agree should have posted it with other. Thank you so much for your help, I'm very greatful. I truly is driving me mad. lol... Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439395 Share on other sites More sharing options...
oraya Posted January 15, 2008 Author Share Posted January 15, 2008 Forgot to say id is set to auto_increment, hence why it's hidden. All the others are just fine though. I've been looking at it for over an hour now, can't see what's wrong. Sigh.... Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439396 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 <?php include_once '../inc/config.php'; include_once 'hearse.php'; // assuming all your fields are required and they cannot be empty foreach($_POST as $key=>$val){ $error = 0; if (!isset($_POST[$key])){ $error ++; } } if($error<=0){ $id =mysql_real_escape_string($_POST['id']); $cat=mysql_real_escape_string($_POST['cat']); $title=mysql_real_escape_string($_POST['title']); $info=mysql_real_escape_string($_POST['info']); $price=mysql_real_escape_string($_POST['price']); $query = "INSERT INTO eco_friendly VALUES ('$cat', '$title', '$info', '$price')"; mysql_query($query); mysql_close(); header("Location: added.php"); exit; } else{ echo 'please fill up the form completely'; } ?> try not tested but should work Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439401 Share on other sites More sharing options...
oraya Posted January 15, 2008 Author Share Posted January 15, 2008 I can't explain this, BUT I just re-wrote the post_add.php and the form, and it's now working. I re-named old as BCKUP and compaired them to the re-writes, they are identical. But the new ones work. How strange is that! Thank you both for you patience and help, you may not know it, but you helped a lot. Well you saved me from becoming bald anyway. lol.... Thank you soooooooo much for trying to help! Best wishes, Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439408 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 is that the new code i gave you? Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439412 Share on other sites More sharing options...
oraya Posted January 15, 2008 Author Share Posted January 15, 2008 Now I just re-wrote the original. I didn't see your code until after I had re-written it. I doesn't make any sense, but there you go. I've gone over it line by line and they are both the same. It's really odd. Thank you for taking the time to write and post the code though. Oraya Quote Link to comment https://forums.phpfreaks.com/topic/86038-solved-undefined-index-issue/#findComment-439419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.