ERROR -- Notice underfinded index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
technology2k
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2003 12:17 pm

ERROR -- Notice underfinded index

Post by technology2k »


I'm getting this error message. Think anyone can help me out??

--> Notice: Undefined index: Email in D:\wwwroot\test\add.php on line 23
-
--> Notice: Undefined index: Name in D:\wwwroot\test\add.php on line 24

LM = LINE NUMBER

LM-|
22-| //Assign contents of form to variables
23-| $email = $_POST['Email'];
24-| $name = $_POST['Name'];
25-|
26-| $sql = "INSERT INTO addressbook SET
27-| Email = '$email', First_Name = '$name'";
28-|
29-| $result = mysql_query($sql);
30-|
31-| //Code to check if statement executed properly and display message
32-| if ($result) {
33-| echo("Email and Name successully added");
35-| } else {
36-| echo("An error has occured");
37-| }

User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

The error means there is no element in the $_POST array called "Email" or "Name", and it is a notice not really an error.

It usually happens when you open a page that data hasn't been submitted to, such as the form handler you have created.

Just make sure you have the array elements in the $_POST array before trying to use them.

Regards,
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and therefor take a look at http://php.net/isset and/or http://php.net/empty
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Cheers Volka :D

Regards,
technology2k
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2003 12:17 pm

Post by technology2k »

[color=red]
I still can't find out what is wrong here is the whole codeing....

----------------
- form.html
----------------

<html>
<head>
<title>FORM</title>
</head>
<body>

<form action="code.php">
Name: <input type="text" name="Name"><br>
Email: <input type="text" name="Email">
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

-------------------------------------------------------------------------
-------------
- code.php
----------------

<html>
<head></head>
<body>
<?
$host = "localhost";

MySQL_connect("$host");

MySQL_select_db("ct2k") or die("Could not select database");

//Line 23 - 24
$email = $_POST['Email'];
$name = $_POST['Name'];
//Line 23 - 24

$sql = "INSERT INTO addressbook SET
Email = '$email', First_Name = '$name'";

$result = mysql_query($sql);

if ($result) {
echo("Email and Name successully added");
} else {
echo("An error has occured");
}

MySQL_close()
?>

</body>
</html>

[/color]
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If the $_POST vars aren't always set when the script is called, you need to set a default value for your vars eg:

Code: Select all

<?php
IF (isset($_POST['email'])) {

    $email = $_POST['email'];

} ELSE {

    $email = null;

   // ..or maybe you need to redisplay the form with a "please enter email" message?
}
?>
Last edited by McGruff on Thu Aug 11, 2005 6:01 am, edited 1 time in total.
technology2k
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2003 12:17 pm

Post by technology2k »

Woud like to say thank you for trying to help me.. I have found out what the problem was. The problem was on the form.html file i forgot to add the method="post".

Not working--> <form action="add.php">

Working--> <form method="post" action="add.php">
Post Reply