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
PHP Notice: Undefined index:
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<?php
if (isset($_POST["submit"]))
{
//assign the vars here
}
?>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.
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.
The @ will only supress eventual warnings...Code: Select all
<?php if (!@$_REQUEST["btnPress"]) { ?>
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;
}