ERROR -- Notice underfinded index
Moderator: General Moderators
-
technology2k
- Forum Newbie
- Posts: 3
- Joined: Thu Jun 12, 2003 12:17 pm
ERROR -- Notice underfinded index
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-| }
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,
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,
and therefor take a look at http://php.net/isset and/or http://php.net/empty
-
technology2k
- Forum Newbie
- Posts: 3
- Joined: Thu Jun 12, 2003 12:17 pm
[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]
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]
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