PHP Notice: Undefined 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
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

PHP Notice: Undefined index:

Post by jonah »

I created an HTML form and PHP script to add records to a database. The
script works and the records are added, but the intermediate variables
cause a PHP Notice: Undefined index: error to appear in the Apache
error_log.

Code:

<?
$ide = $_POST["Ide"];
$usr = $_POST["User"];
$pas = $_POST["Pass"];
$pin = $_POST["Pin"];
$alt = $_POST["Alt"];
$lot = $_POST["Lot"];

$db = mysql_connect("localhost","root");
mysql_select_db("list",$db);
$query="INSERT INTO Table1 (Ident,Username,Password,Pin,Alt,Lot) VALUES ('$ide','$usr','$pas','$pin','$alt','$lot')";
mysql_query($query,$db);

mysql_close();
?>

I have searched this forum to an answer to this question but have
found nothing satisfactory, even running to turning off error notices.

I don't think turning off error notices is the way to go, so I would
like to find the cause of these notices and alter the code accordingly.

Any suggestions?

TIA
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

AFAIK, the apache log will pretty much always have them, unless you actually get rid of the problem...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why not only use the variable if it is defined then, get rid of the problem you should of never had :P
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Post by jonah »

The problem occurs because the php code is included in the same
file as the form and so runs when the page is first loaded and there
is no input. Thus the variables are undefined until the form values
are submitted.

I suppose I could create another file which includes only the code
which would then run only when called from the form page with the
action="action.php" tag in the form. However, I always try to limit
my forms and code to a single file.

I suppose the best way to accomplish this objective is to put the code
in a function which could then be called by the 'action' statement.
I'm surprised that you didn't mention this or that I didn't think of it
sooner.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php

if (isset($_POST["submit"]))
{
//assign the vars here
}

?>
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Post by jonah »

Yes, 'if else' actually works better.

It doesn't seem to be posible to design a function in php which
will not run when the page is loaded. Any code would have
to be included in a separate 'action' file.

Fortunately, the error messages can be turned off individually
with @. I decided to use something like:

if (!@$_REQUEST["btnPress"]) {

Maybe I don't need @ if I use isset; I'll try it.

Thanks for your help.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

<?php

if (!@$_REQUEST["btnPress"]) { 

?>
The @ will only supress eventual warnings...

The cleaneast way to test if something has been posted or not is like:

Code: Select all

if (isset($_POST['name_of_submit_button']))
{
     // clean input
     $val1 = isset($_POST['val1']) ? mysql_escape_string($_POST['val1'] : 0;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

warning: don't check for the submit button only.. as the page could be submitted correctly without hitting a button.
Post Reply