• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2393
  • Last Modified:

Undefined Variable

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
DarrenJackson
Asked:
DarrenJackson
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
ldbkuttyCommented:
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
 
ldbkuttyCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
techtonikCommented:
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
 
Marcus BointonCommented:
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
 
Rajkumar_GCommented:
(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
 
DarrenJacksonAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now