0

OK I am aware there are similar questions being asked/answered here on stack, but I tried everything suggested in those threads to no avail. As the title suggests I'm getting the undefined index notice when I attempt to view the xml written by the PHP script below namely for "track_number" and "track_title". I know these columns exist in my database. I suspect something is wrong where I query the tracklist table, but can not figure out just what. Any ideas would be appreciated!

PHP

<?php
ini_set('display_errors',1);


define("HOST",'54.152.129.000');
$db=mysqli_connect(HOST,'wp_user','password','*******');

$query="select * from albums";
$result=mysqli_query($db,$query);

$fp=fopen('music_inventory.xml','w');
$header="<?xml version=\"1.0\"?>\n<!DOCTYPE album SYSTEM \"music_inventory.dtd\">\n";
$header.="<?xml-stylesheet type=\"text/css\" href=\"music_inventory.css\"?>\n";

$written=fwrite($fp,$header);

$xml_data="<music_inventory>";


while($row=mysqli_fetch_array($result))
{
    $xml_data .= "<album id=\"".$row['id']."\" type=\"".$type."\"   albumart=\"".$row['albumart']."\">";
    $xml_data .= "<artist>".htmlentities($row['artist'])."</artist>";
    $xml_data .= "<name>".$row['name']."</name>";
    $xml_data .= "<year>".$row['year']."</year>";
    $xml_data .= "<label>".$row['label']."</label>";
    $xml_data .= "<disc>".$row['disc']."</disc>";
    $xml_data .= "<totaldiscs>".$row['totaldiscs']."</totaldiscs>";


        switch($row["type"])
        {
                case 'E':
                        $type='EP';
                        break;
                case 'F':
                        $type='full_length';
                        break;
                case 'S':
                        $type='soundtrack';
                        break;
                case 'C':
                        $type='compilation';
                        break;
                case 'M':
                        $type='multi_disc';
                        break;
                default:
                        $type='unknown';
                        break;
        }

        $xml_data .=  "<tracklist>";

        //problem somewhere in here?
        $track_query="select track_number,track_title,track_artist from tracklist where id='" . $row['id'] . "' order by track_number";
        $track_result=mysqli_query($db,$track_query);
        while($track_row=mysqli_fetch_array($track_result))
        {

            $xml_data .= "<track id=".$row['track_number'].">".htmlentities($row['track_title'])."</track>";

        }
        $xml_data .= "</tracklist>";
        $xml_data .= "</album>";
}




$xml_data .= "</music_inventory>";


$written=fwrite($fp,$xml_data);

fclose($fp);

readfile('music_inventory.xml');

?>
1
  • Please add the structure from albums and tracklist just to make certain you did not overlook the obvious (the situation of staring to long at the screen). Commented Mar 23, 2015 at 1:33

2 Answers 2

3

Probably a typo? You're looping over this:

while($track_row=mysqli_fetch_array($track_result))

but you never reference $track_row. Maybe you meant this?

$xml_data .= "<track id=".$track_row['track_number'].">".htmlentities($track_row['track_title'])."</track>";

The parent loop uses $row, but that's from a different query. One which apparently doesn't have those fields.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh yes, a result of me being lazy! I copy/pasted from the parent loop and overlooked my mistake... for the entire day. Thank you!
1
while($track_row=mysqli_fetch_array($track_result))
    {

        $xml_data .= "<track id=".$row['track_number'].">".htmlentities($row['track_title'])."</track>";

    }

I only briefly looked at this but you are using $row not $track_row in the while loop.

1 Comment

You both nailed it right on the head, but David was 15 seconds faster. I gotta give it to him. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.