Jump to content

Undefined Index question ** SOLVED **


loringe

Recommended Posts

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>
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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 above
if(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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.