[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Undefined Variable

Posted on 2004-08-29
7
Medium Priority
?
2,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 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