简体   繁体   中英

Validation Error: Bad value for attribute href on element a: Illegal character in query: not a URL code point

I am pulling database information and displaying it in a table, the content of the title column is linked to a dynamic page. It all works perfectly but when validating my output page with the W3C Validator I get the following error and have no idea how to fix it!

Bad value newpage.php?url= DATABASE INFORMATION for attribute href on element a: Illegal character in query: not a URL code point.

It then highlights the end tag for the a href ">".

<?php 
    echo"<table>
    <tr>
        <th>Title</th>
        <th>A</th>
        <th>B</th>
        <th>C</th>
        <th>D</th>
    </tr>
    </table>";


        if (mysqli_num_rows($SQLresult) == 0 ) 
        { 
            echo 'No results found, please try again.';
        } 
        else 
        {   
            while ($row = mysqli_fetch_assoc($SQLresult)) 
            {
                echo "
                <table>
                <tr>
                    <td><a href='newpage.php?url=".$row['title']."'>".$row['title']."</td>
                    <td>".$row['A']."</td>
                    <td>".$row['B']."</td>
                    <td>".$row['C']."</td>
                    <td>".$row['D']."</td>
                </tr>
                </table>";
            }          
        }
?>

Any help would be greatly appreciated, thanks.

Not all characters are legal in URLs. Those that are not legal can be recoded using the percent sign and two hex digits. PHP's urlencode() function does that. Use urlencode() on the part that is to be the link.

$url = urlencode($row['title']);
...
 <td><a href='newpage.php?url=".$url."'>".$row['title']."</td>

Alternatively, you could do this:

<td><a href='newpage.php?url=".urlencode($row['title'])."'>".$row['title']."</td>

You can find more information about legal characters in URL/URI in RFC2396: https://tools.ietf.org/html/rfc2396#section-2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM