PHP XML RSS error xmlParsePI : no target name-ThrowExceptions

Exception or error:

Please help! Running into this error:
error on line 11 at column 3: xmlParsePI : no target name.

Line 11 is the atom:link tag. I tried changing the link to be the actual file name and got the same error. I’m running the code locally so I also tried using the localhost url and still the same error.

I am new to php and this is an assignment for a php course with what I believe is outdated material. Any advice will be helpful!

<?php
require_once('./Includes/connectvars.php');
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
$builddate = gmdate(DATE_RSS, time());
?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Vintage Cars and Trains</title>
        <atom:link href="http://<?= $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'] ?>" rel="self" type="application/rss+xml" />
        <link>products.php</link>
        <description>Vintage cars and trains for lesson 9</description>
        <lastBuildDate><?php $builddate ?></lastBuildDate>
        <language>en-us</language>

<?

    $query = " SELECT * FROM products WHERE productLine = 'Vintage Cars', 'Trains' ORDER BY dateAdded DESC LIMIT 10 ";
    $result = mysqli_query($dbc, $query)
        or die("Error querying database => $query");

    //Loop through and print data
    while ($newArray = mysqli_fetch_array($result)) {
        $product_code = $newArray[productCode];
        $product_name = $newArray[productName];
        $product_line = $newArray[productLine];
        $product_scale = $newArray[productScale];
        $product_vendor = $newArray['productVendor'];
        $product_description = $newArray['productDescription'];
        $buy_price = $newArray['buyPrice'];

        $pubdate = date(DATE_RSS, strtotime($date_added));
?>

    <item>
        <title><?php echo "$product_code - $product_name"; ?></title>
        <description><?php $description ?></description>
        <link><link>localhost/PHP/Lesson9/products.php?pid=<?php $product_code ?></link>
        <guid isPermaLink="false">localhost/PHP/Lesson9/products.php?pid=<?php $product_code ?><guid>
        <pubDate><?php $pubdate ?></pubDate>
    </item>

    }

    </channel>
</rss>
How to solve:

When you output a php variable, please make sure that you use:

<?php echo $variablename; ?>

When you want to concatenate two php variables, please make sure that you use a period:

<?php echo $variablename1.' - '.$variablename2; ?>

Please remember to echo and put a semicolon at the end of each php inlines:

<?php echo something; ?>

Please remember to close while loop with curly bracket where the loop ends:

<?php
     while (condition) {
           do something;
     }
?>

Here’s the cleaned up code.

<?php
require_once('./Includes/connectvars.php');
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
$builddate = gmdate(DATE_RSS, time());
?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
    <title>Vintage Cars and Trains</title>
    <atom:link href="http://"<?php echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>" rel="self" type="application/rss+xml" />
    <link>products.php</link>
    <description>Vintage cars and trains for lesson 9</description>
    <lastBuildDate><?php echo $builddate; ?></lastBuildDate>
    <language>en-us</language>

<?php

$query = " SELECT * FROM products WHERE productLine = 'Vintage Cars', 'Trains' ORDER BY dateAdded DESC LIMIT 10 ";
$result = mysqli_query($dbc, $query)
    or die("Error querying database => $query");

//Loop through and print data
while ($newArray = mysqli_fetch_array($result)) {
    $product_code = $newArray[productCode];
    $product_name = $newArray[productName];
    $product_line = $newArray[productLine];
    $product_scale = $newArray[productScale];
    $product_vendor = $newArray['productVendor'];
    $product_description = $newArray['productDescription'];
    $buy_price = $newArray['buyPrice'];
}

$pubdate = date(DATE_RSS, strtotime($date_added));
?>

<item>
    <title><?php echo $product_code.' - '.$product_name; ?></title>
    <description><?php echo $description; ?></description>
    <link><link>localhost/PHP/Lesson9/products.php?pid=<?php echo $product_code; ?></link>
    <guid isPermaLink="false">localhost/PHP/Lesson9/products.php?pid=<?php echo $product_code; ?><guid>
    <pubDate><?php echo $pubdate; ?></pubDate>
</item>

</channel>
</rss>

Leave a Reply

Your email address will not be published. Required fields are marked *