Undefined Variable in php


Undefined Variable in php



I have inherited some code for a custom CMS that is a little out of my league and keep stumbling over the same errors, Notice: Undefined variable: media in /Applications/MAMP/htdocs/Chapman/Chapman_cms/admin/team-2.php on line 48. This is supposed to create new users and edit old users. However, it does not work when I try and add a new user.

Below is the pertinant code:

$db = new database("mysql",$dbHost,$dbName,$dbUser,$dbPass); $target = 'add'; if ($_GET['task'] == 'edit') { $media = $db->get_row(edit_media_item($db, $_GET['team_id'])); $target = 'update';  <p><label for="copy">Full Name:</label>     <input type="text" name="title" value="<?=$media['title']?>" />     <textarea name="media" id="media" cols="30" rows="5" style="width: 100%"><?=$media['copy']?></textarea></p>     <input type="hidden" name="process" value="<?=$target.",copy,4,team-1,".$media['id'].""?>">     <p><input type="submit" name="save" value="Submit" />     <input type="reset" name="reset" value="Reset" /></p>     </form> 

Any help would be much appreciated.




Changing timezone in PHP

1:



PHP Automatically “GET” Variables
To remove the notice in the right way is to do this with the code.
htaccess request forwarding if internal ip
<?php if(isset($media['copy'])){ echo $media['copy']; } ?> 


Method to Serialize Objects from appserver to web server without code duplication


PHP Convert HTML Formatted Date

2:



How to use MySQL functions in Propel
It may be hard to help you like this, but, I would see where this $db->get_row() call goes and what it returns (using var_dump() or something...).
ordering a list of files in a folder using php
As general tip, I would recommend setting up debugger in your system, so you can trace calls.


Storing Passwords in reversible form
On windows platform I use xdebug with WinCacheGrind to trace call when I am unsure about call hierarchy.

On Linux, setup is similar (xdebug,kcachegrind...)..


3:


The notice is irrelevant, but this code doesn't create anything.

That happens on the page it is submitted to.

Look at the if statement on the first few lines.

I guess you need to call it with task=edit in the URL..


4:


you can also use the at symbol like this:.
if($_GET['undefined_key']) {     // blah... 

} if(@$_GET['undefined_key']) { // blah...

}
it suppresses warnings, however some will argue that the best time to use the at symbol is to avoid warnings you couldn't do otherwise..


5:


The code you posted does not do any creating, so that problem does not stemfrom this bit of code.. The undefined notice is from the <?=$media['copy']?> bit.

$media was never defined.

If this is not an issue, ignore it and tell PHP to not output notices.

This isn't exactly good practice, but if you're not getting paid to fix every little thing, I'd say it's a feasible alternative.. To suppress notices add this code anywhere before the notices occur or better yet into a global include:.
error_reporting(E_ERROR | E_WARNING | E_PARSE); 
For more info: http://www.php.net/error_reporting.


6:


That error message is not from this code.. $media is assigned in line 6 of the code you provided ($media = $db->get_row(..)).

I'm guessing that you either have stripped out the relevant code (Which is line 48, give/take), or it's the wrong file (Is this from /Applications/MAMP/htdocs/Chapman/Chapman_cms/admin/team-2.php?)..



75 out of 100 based on 85 user ratings 585 reviews

@