944,014 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7581
  • PHP RSS
Mar 21st, 2007
0

undefined variable error

Expand Post »
Hey, I've just uploaded some stuff to server and it works fine but theres a big error message through the screen saying "undefined variable seacrh at line 15"
Code:
[php]if(isset($_GET['search']))
if(!isset($cmd))
{
$search = $_GET['search'];
}
$keywords = explode(" ", $search); <--- line 15
[/php]

Also is there any way to write to an outfile (SQL) on my server. It worked offline but my absolute path isn't working. I assume the directory can't be written to.

Thanks for any help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Mar 21st, 2007
0

Re: undefined variable error

Well..maybe you need something between " and " in line 15?

PHP Syntax (Toggle Plain Text)
  1. $keywords = explode("$string", $search);

Like that maybe?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Andy-Pandy is offline Offline
14 posts
since Oct 2006
Mar 21st, 2007
0

Re: undefined variable error

I don't thinks so. I think it has something to do with isset, if is set the variable before the code uses it, the error message goes away but new ones appear for different variable (same message though). Does anyone think it may be differences between php4 and 5?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Mar 22nd, 2007
0

Re: undefined variable error

You get the error message because you have your error level set to E_ALL or simalar. The message that you are getting is a warning and doesn't affect how the script runs.

When you put it on a production server you shoud have the lower level warning messages turned off anyway. If you really want to get ride of it just intialise the variable at the start of the script.
Reputation Points: 10
Solved Threads: 4
Light Poster
UrbanSky is offline Offline
42 posts
since Oct 2006
Mar 22nd, 2007
0

Re: undefined variable error

Ok, the problem is my hosting does not let me change the php.ini file. Can I change the error level in the script say with set_error_handler(). Would I just put at the top of the script.
Cheers for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Mar 22nd, 2007
0

Re: undefined variable error

Click to Expand / Collapse  Quote originally posted by UrbanSky ...
You get the error message because you have your error level set to E_ALL or simalar. The message that you are getting is a warning and doesn't affect how the script runs.

When you put it on a production server you shoud have the lower level warning messages turned off anyway. If you really want to get ride of it just intialise the variable at the start of the script.
Do it as UrbanSky said. It would look something like this:
[PHP]$search = '';
if(isset($_GET['search']))
if(!isset($cmd))
{
$search = $_GET['search'];
}
[/PHP]
Reputation Points: 17
Solved Threads: 14
Posting Whiz
DanceInstructor is offline Offline
355 posts
since Feb 2005
Mar 22nd, 2007
0

Re: undefined variable error

Thanks, that fixed the undefined variables but how do i fix the undefined indexes I'm getting on other pages? Code;
[php]$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);[/php] is giving undefined indexes txtmessage and txtname.

While[php]if($_GET["cmd"]=="delete")[/php] is giving cmd

and finally[php]if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")[/php] is also giving cmd and I don't know how to define an index.

Thanks very much for any help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Mar 22nd, 2007
0

Re: undefined variable error

You are getting these because you are referancing array keys that do not exist.

Without seeing your whole script it is difficult to give you definate answer but I would assume that you are using the $_POST and $_GET variables each time the script is run wheater a form has been submited or not (which I guess means that you form submits to the page that it is on).

The easiest way to stop this is check to see if the form has ben submitted with something like
PHP Syntax (Toggle Plain Text)
  1. if(isset($_POST['submit']))

Or you are referencing form fields that do not exist.
Reputation Points: 10
Solved Threads: 4
Light Poster
UrbanSky is offline Offline
42 posts
since Oct 2006
Mar 24th, 2007
0

Re: undefined variable error

You could use the shorthand notation with isset, it keeps things on one line..

[php]
$name = isset($_POST['txtName']) ? stripslashes($_POST['txtName']) : '';
[/php]
Last edited by digital-ether; Mar 24th, 2007 at 6:47 am.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: find the parse error
Next Thread in PHP Forum Timeline: PHP - subscribers' email





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC