tags:

views:

710

answers:

6

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.

+1  A: 

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.

MattW.
+2  A: 

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...)

As general tip, I would recommend setting up debugger in your system, so you can trace calls. 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...).

f13o
A: 

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

_Lasar
+5  A: 

To remove the notice in the right way is to do this with the code

<?php if(isset($media['copy']){ echo $media['copy']; } ?>
Ólafur Waage
+1  A: 

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.

SeanDowney
Cool tip, I didn't realize you could use that for variables...only thought it was for function calls.
Matt Huggins
A: 

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?).

troelskn