Page 1 of 1

PHP Notice: Undefined index:

Posted: Wed Aug 25, 2004 9:25 pm
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

Posted: Wed Aug 25, 2004 9:28 pm
by feyd
AFAIK, the apache log will pretty much always have them, unless you actually get rid of the problem...

Posted: Wed Aug 25, 2004 10:22 pm
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

Posted: Wed Aug 25, 2004 11:26 pm
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.

Posted: Wed Aug 25, 2004 11:39 pm
by John Cartwright

Code: Select all

<?php

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

?>

Posted: Thu Aug 26, 2004 12:38 pm
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.

Posted: Thu Aug 26, 2004 12:54 pm
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;
}

Posted: Thu Aug 26, 2004 1:20 pm
by feyd
warning: don't check for the submit button only.. as the page could be submitted correctly without hitting a button.