undefined variable error

Reply

Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

undefined variable error

 
0
  #1
Mar 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: Andy-Pandy is an unknown quantity at this point 
Solved Threads: 0
Andy-Pandy Andy-Pandy is offline Offline
Newbie Poster

Re: undefined variable error

 
0
  #2
Mar 21st, 2007
Well..maybe you need something between " and " in line 15?

  1. $keywords = explode("$string", $search);

Like that maybe?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: undefined variable error

 
0
  #3
Mar 21st, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: UrbanSky is an unknown quantity at this point 
Solved Threads: 4
UrbanSky UrbanSky is offline Offline
Light Poster

Re: undefined variable error

 
0
  #4
Mar 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: undefined variable error

 
0
  #5
Mar 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: undefined variable error

 
0
  #6
Mar 22nd, 2007
Originally Posted by UrbanSky View Post
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]
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: undefined variable error

 
0
  #7
Mar 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: UrbanSky is an unknown quantity at this point 
Solved Threads: 4
UrbanSky UrbanSky is offline Offline
Light Poster

Re: undefined variable error

 
0
  #8
Mar 22nd, 2007
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
  1. if(isset($_POST['submit']))

Or you are referencing form fields that do not exist.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: undefined variable error

 
0
  #9
Mar 24th, 2007
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC