验证错误:元素 a 上的属性 href 值错误:查询中的非法字符:不是 URL 代码点


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

我正在提取数据库信息并将其显示在表格中,标题列的内容链接到动态页面。这一切都运行良好,但是在使用 W3C 验证器验证我的输出页面时,我收到以下错误,不知道如何解决它!

错误值 newpage.php?url= 元素 a 上的属性 href 的数据库信息:查询中的非法字符:不是 URL 代码点。

然后,它突出显示 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>";
            }          
        }
?>

任何帮助将不胜感激,谢谢。

并非所有字符在 URL 中都是合法的。 那些不合法的可以使用百分号和两个十六进制数字重新编码。 PHP 的 urlencode() 函数就是这样做的。在要作为链接的部分上使用urlencode()

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

或者,您可以这样做:

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

您可以在 RFC2396 中找到有关 URL/URI 中合法字符的更多信息:https://www.rfc-editor.org/rfc/rfc2396#section-2