Solved

Undefined Variable

Posted on 2004-08-29
7
2,362 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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:Marcus Bointon
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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