exeleon
exeleon

Reputation: 121

Undefined index PHP/mySQL

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');

?>

Upvotes: 0

Views: 84

Answers (2)

Burrito
Burrito

Reputation: 515

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.

Upvotes: 1

David
David

Reputation: 218808

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.

Upvotes: 3

Related Questions