如何访问我的网址中的slug值?

问题描述 投票:0回答:2

我想知道为什么我的代码没有显示任何结果。

我在home.php页面中有一个链接,如下所示:

<a href="../musica/'.$row['slug'].'">Click here</a>`

musica.php页面中,我访问了slug:

$slug = $_GET['slug'];

我的查询看起来像这样:

select * from musical layers where slug = $slug

但它没有返回任何东西。

这是我的超链接所针对的完整网址:

scrashmusicas.epizy.com/musica/wesley-safadao-banda-garota-safada

没有错误,但我无法获得slug值。谁能帮我?

这是我的musica.php文件的相关部分:

$slug = $_GET['slug'];
$result_capa = "select * from capas_musicas where slug = $slug";
$resultados_capa = mysqli_query($conn, $result_capa);
while($row = mysqli_fetch_assoc($resultados_capa)){
    echo '
        <li>
            <a href="../musica/'.$row['slug'].'">
                <div class="box_destaque">
                    <img src="'.$row['capa_album'].'" border="0"/>
                    <strong>'.$row['nome_album'].'</strong>
                    <span>'.$row['categoria_album'].'</span>
                </div>
            </a>
        </li>
    ';
}
php url slug
2个回答
-2
投票

有几件事要解决......

$slug = $_GET['slug'];

如果/当slug超全球中没有$_GET密钥时,您将收到通知。在尝试访问/使用该值之前,您需要检查它是否为isset()

$result_capa = "select * from capas_musicas where slug = $slug";

此查询不安全。您需要在占位符中使用预准备语句,每次在查询中使用用户提供的或通常不安全的数据。对于您当前的代码,$slug需要至少单引号包装(除非它当然是一个整数 - 但仍然使用准备好的语句)

至于$_GET,你没有提供href值的查询字符串,所以没有任何东西通过$_GET传递。

$_SERVER['REQUEST_URI']脚本上调用musica.php将产生此字符串值:

/musica/wesley-safadao-banda-garota-safada

要访问slug值,请使用:

$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
$slug=substr($path,strrpos($path,'/')+1);

要不就:

$slug=substr($_SERVER['REQUEST_URI'],8);  // access substring after "/musica/"

1
投票

尝试更改为:

<a href="../musica/?slug='.$row['slug'].'">Click here</a>
© www.soinside.com 2019 - 2024. All rights reserved.