Page 1 of 1

ERROR -- Notice underfinded index

Posted: Thu Jun 12, 2003 12:17 pm
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-| }


Posted: Thu Jun 12, 2003 1:00 pm
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,

Posted: Thu Jun 12, 2003 1:30 pm
by volka
and therefor take a look at http://php.net/isset and/or http://php.net/empty

Posted: Thu Jun 12, 2003 4:32 pm
by cactus
Cheers Volka :D

Regards,

Posted: Thu Jun 12, 2003 5:39 pm
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]

Posted: Thu Jun 12, 2003 5:45 pm
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?
}
?>

Posted: Thu Jun 12, 2003 6:14 pm
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">