loringe Posted November 4, 2006 Share Posted November 4, 2006 I'm very new to PHP and I'm trying to get simple form to post the data, however I can't get around this "undefined index" error. I would like to understand why?<html><head></head><body><form method="POST" action=""><input type=text name=my_first value=""><input type=text name=my_last value=""><input type=submit value="Submit"></form><?php$db_host='localhost';$db_database='users';$db_username='tree';$db_password='school';$my_first=$_POST['my_first'];$my_last=$_POST['my_last'];mysql_connect($db_host,$db_username,$db_password);mysql_select_db($db_database) or die( "Unable to select database");$query = "INSERT INTO contacts VALUES ('$my_first','$my_last')";mysql_query($query);mysql_close();?></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/ Share on other sites More sharing options...
fiddy Posted November 4, 2006 Share Posted November 4, 2006 Can you pls paste the error.i guess some thing is wrong here:$query = "INSERT INTO contacts VALUES ('$my_first','$my_last')";mysql_query($query);try like this:$query = "INSERT INTO contacts (field1,field2) VALUES ('$my_first','$my_last')";mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119500 Share on other sites More sharing options...
toplay Posted November 4, 2006 Share Posted November 4, 2006 When your script first runs, the form has not been submitted so there's no "my_first" and "my_last" array index values.Put quotes surrounding the name values:<input type=text name="my_first" value=""><input type=text name="my_last" value="">Action is missing a value. Example:<form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">Give submit a name:<input type=submit value="Submit" name="submit">You need to only insert into the table when the form has been submitted. Example:$submitted = isSet($_POST['submit']) ? TRUE : FALSE;if ($submitted) { // Do all the insert code in here $my_first = isset($_POST['my_first']) ? $_POST['my_first'] : ''; $my_last= isset($_POST['my_last']) ? $_POST['my_last'] : '';}It's usually a good idea to use isset() to make sure an index exists/defined before trying to use it's value.Also, it's good coding to specify column names on the INSERT along with the VALUES. Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119502 Share on other sites More sharing options...
loringe Posted November 4, 2006 Author Share Posted November 4, 2006 This is what I'm getting for an error...thanks for the help :)PHP Notice: Undefined Index: my_first in C:\....hell.php on line 16PHP Notice: Undefined Index: my_last in C:\....hell.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119504 Share on other sites More sharing options...
loringe Posted November 4, 2006 Author Share Posted November 4, 2006 Thanks so much :) Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119505 Share on other sites More sharing options...
zbrahead Posted November 4, 2006 Share Posted November 4, 2006 Undefined index means that there isnnt any POST data going to the page, should you put the ACTION of the form to PHP self you should be rockin' Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119543 Share on other sites More sharing options...
wildteen88 Posted November 4, 2006 Share Posted November 4, 2006 [quote author=loringe link=topic=113806.msg462785#msg462785 date=1162616557]I'm very new to PHP and I'm trying to get simple form to post the data, however I can't get around this "undefined index" error. I would like to understand why?<html><head></head><body><form method="POST" action=""><input type=text name=my_first value=""><input type=text name=my_last value=""><input type=submit value="Submit"></form><?php$db_host='localhost';$db_database='users';$db_username='tree';$db_password='school';$my_first=$_POST['my_first'];$my_last=$_POST['my_last'];mysql_connect($db_host,$db_username,$db_password);mysql_select_db($db_database) or die( "Unable to select database");$query = "INSERT INTO contacts VALUES ('$my_first','$my_last')";mysql_query($query);mysql_close();?></body></html>[/quote]You are always going to retrieve the undefined notice message when you go to hell.php and you haven't submitted the form as no POST data has been sent. What you should of done is add an if statement which checks whether the forum has been submitted. If it has been submitted then you can use the $_POST vars. So your code should ideally look something like the following:[code]<html><head></head><body><form method="POST" action=""><input type="text" name="my_first" value=""><input type="text" name="my_last" value=""><input type="submit" name="submit_btn" value="Submit"></form><?php// check that form has been submitted:// we this by checking whether the $_POST['submit_btn'] variable exists// if it does then the form has been submitted// submit_btn is the name of our submit button I // added name="submit_btn" to your HTML aboveif(isset($_POST['submit_btn'])){ $db_host = 'localhost'; $db_database = 'users'; $db_username = 'tree'; $db_password = 'school'; $my_first = $_POST['my_first']; $my_last = $_POST['my_last']; mysql_connect($db_host,$db_username,$db_password); mysql_select_db($db_database) or die( "Unable to select database"); $query = "INSERT INTO contacts VALUES ('$my_first','$my_last')"; mysql_query($query); mysql_close();}?></body></html>[/code]Now what will happen is when you first go to hell.php PHP will now check whether the $_POST['submit_btn'] variable exists. If it does it'll run the code between [code=php:0]if(isset($_POST['submit_btn'] {[/code] and [code=php:0]}[/code]. If it doesn't then the code wont be run and the undefined index notice will not appear.If you doon't do this every time you go to hell.php and you haven't submitted the form. PHP will always execute the code below the form and report an undefined index notice and add blank values in to your database. Which is obviously not what you want. If it is then it is bad programming. Quote Link to comment https://forums.phpfreaks.com/topic/26131-undefined-index-question-solved/#findComment-119577 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.