Solved

Undefined Variable

Posted on 2004-08-29
7
2,360 Views
Last Modified: 2008-02-01
I am after somone who knows PHP

I am new to PHP code I am trying to apply the code below to a webpage which in the past has worked fine on another server i dont have access to this server anymore as it crashed and we where not able to retrieve the data so i am having to re-write the webpage. On my webpage i get error messages.

undefined variable submit C:\Inetpub\wwwroot\addressbook\edit.php on line 88
undefined variable updateC:\Inetpub\wwwroot\addressbook\edit.php on line 94
undefined variable ID C:\Inetpub\wwwroot\addressbook\edit.php on line 100

Here is the part of the code that the error messages refer to

if($submit)
{
$sql = "INSERT INTO $table (firstname, lastname, address, home, mobile, work, email, email2, bday, bmonth, byear) VALUES ('$firstname','$lastname','$address','$home','$mobile','$work','$email','$email2','$bday','$bmonth','$byear')";
$result = mysql_query($sql);
echo "<br><br>Information entered into address book.\n";
}
else if($update)
{
$sql = "UPDATE $table SET firstname='$firstname',lastname='$lastname',address='$address',home='$home',mobile='$mobile',work='$work',email='$email',email2='$email2',bday='$bday',bmonth='$bmonth',byear='$byear' WHERE id=$id";
$result = mysql_query($sql);
echo "<br><br>Address book updated.\n";
}
else if($id)
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);


The only thing that comes to my mind is that the version of PHP has changes and so has the code php 4.3.8 is currently installed should i be using an older version or can this code be updates to work with the latest.

That is if this is the problem.


Can anyone help

I will award 500 points to anyone with an answer


Cheers

Darren
0
Comment
Question by:DarrenJackson
7 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 11926599
Seems like a globals config problem. You can use

if (isset($submit))

It will be better to use

if (!empty($_POST['submit']), will work always

If you plan to use globals, also write session_start() at the beginning of each php file

Good luck,
Jaime.
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 250 total points
ID: 11926605
undefined varible is a notice error.

If you have not supressed notice errors in your php.ini (which is not recommended) you will get these errors.
You can remove by placing isset() function. Try this code instead of yours:

if(isset($submit))
{
$sql = "INSERT INTO $table (firstname, lastname, address, home, mobile, work, email, email2, bday, bmonth, byear) VALUES ('$firstname','$lastname','$address','$home','$mobile','$work','$email','$email2','$bday','$bmonth','$byear')";
$result = mysql_query($sql);
echo "<br><br>Information entered into address book.\n";
}
else if(isset($update))
{
$sql = "UPDATE $table SET firstname='$firstname',lastname='$lastname',address='$address',home='$home',mobile='$mobile',work='$work',email='$email',email2='$email2',bday='$bday',bmonth='$bmonth',byear='$byear' WHERE id=$id";
$result = mysql_query($sql);
echo "<br><br>Address book updated.\n";
}
else if(isset($id))
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);

and i hope the code gets exectued after submitting a form. So, better *use*

isset($_POST['submit']) instead of isset($submit) or $submit.

Similarly for others.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11926620
Didnt noticed your comment jaime.

To put it all together:

if(isset($_POST['submit']))
{
$sql = "INSERT INTO $table (firstname, lastname, address, home, mobile, work, email, email2, bday, bmonth, byear) VALUES ('$firstname','$lastname','$address','$home','$mobile','$work','$email','$email2','$bday','$bmonth','$byear')";
$result = mysql_query($sql) or die("Insert Sql Error : " . mysql_error());
echo "<br><br>Information entered into address book.\n";
}
else if(isset($_POST['update']))
{
$sql = "UPDATE $table SET firstname='$firstname',lastname='$lastname',address='$address',home='$home',mobile='$mobile',work='$work',email='$email',email2='$email2',bday='$bday',bmonth='$bmonth',byear='$byear' WHERE id=".$_POST['id'];
$result = mysql_query($sql)  or die("Update Sql Error : " . mysql_error());
echo "<br><br>Address book updated.\n";
}
else if(isset($_POST['id']))
{
$result = mysql_query("SELECT * FROM $table WHERE id = ".$_POST['id'],$db);
$myrow = mysql_fetch_array($result)  or die("Select Sql Error : " . mysql_error());
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Expert Comment

by:techtonik
ID: 11926622
This problem is well described in http://php.net/manual/en/security.registerglobals.php

In short - from version 4.2.0 your php.ini on server contains string register_globals = off
which is recommended value for securite reasons. So now variables $submit, $Id etc.
which was submitted via form are not transformed into variables automatically, but
are contained in $_REQUEST array. You may want to turn on register_globals or modify
you script by adding these strings for each undefined variable at the beginning:

$submit = $_REQUEST['submit'];
$ID = $_REQUEST['ID'];
...
0
 
LVL 25

Expert Comment

by:Squinky
ID: 11926984
I don't like this syntax:

if(isset($_POST['submit']))

because it's quite common to use posted vars that have no value, e.g. checkboxes without a value assigned. For example, if $_POST['submit'] exists but is set to null, that test will give a result that's not what you're looking for. It's also likely that it will raise an undefined index notice that isset doesn't otherwise suppress. I prefer:

if(array_key_exists('submit', $_POST))

as there is no potential for any kind of notice or warning, and it actually represents the real state of affairs. If you want to test for a value, then you can do so after this, safe in the knowledge that it does actually exist.
0
 
LVL 2

Expert Comment

by:Rajkumar_G
ID: 11929963
(html)Form values should be get in the php using $_GET or $_POST or $_REQUEST.
If your submit button name is submit_form(in the html form), then you have to get
 the value of the submit button using $_GET['submit_form'] or $_POST['submit_form']
or by using $_REQUEST['submit_form'] according to the method used in the form tag
of html form. similarly for update and select query button too. Whle using submit button,
keep in mind that you can't use more than one submit button in a single form.If your error
is with the submit , update and id then this will be the problem. If you still have problem with
inserting or updating data, then  you have to change your insert query as follows:


$sql = "INSERT INTO $table (firstname, lastname, address, home, mobile, work, email, email2, bday, bmonth, byear) VALUES ($firstname,$lastname,$address,$home,$mobile,$work,$email,$email2,$bday,$bmonth,$byear)";

$result = mysql_query($sql);

Because values will not be parsed in single quotes in PHP.
But it will parse in double quotes.
check with that.
0
 

Author Comment

by:DarrenJackson
ID: 11934738
Thanks all for the help

The replies from both jaime olivares  and idbkutty which were only minutes apart are spot on to my problem so i will split the points.

But thankyou all for helping me out

Cheers


Darren
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now