从url到php变量的id

从url到php变量的id,php,mysql,Php,Mysql,我面临着下一个问题: 我每篇文章都有一个唯一的id,但我不知道如何在while循环到SELECT*的下一页中询问它,其中ask_id=x。现在,href(链接)的id应该添加到x。实际上,我只想把搜索栏中的id转换成变量x <?php $statement = $pdo2->prepare("SELECT * FROM fragen ORDER BY fragen_date DESC"); $result = $statement->execute(); w

我面临着下一个问题: 我每篇文章都有一个唯一的id,但我不知道如何在while循环到SELECT*的下一页中询问它,其中ask_id=x。现在,href(链接)的id应该添加到x。实际上,我只想把搜索栏中的id转换成变量x

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen ORDER BY fragen_date DESC");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<a class="kein" href="forum_forum.php?id='.$row['fragen_id'].'"><div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subt  itel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "br><br>",'<div>'.$row['fragen_id'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div></a>';
    //the variable $chosen_one should get the value of the id   

    }               
?>

如您所见,它正确地生成了url的id

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen WHERE ");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subitel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "<br><br>",'<div class="forum_Bild"><img class="pic" alt="Ein Bild" title="Eine Pflanzen" src="'.$row['fragen_picture'].'">',
        "<br><br>","Bildlink:  ".$row['fragen_picture'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div>';

}
?>

现在,它应该在文档顶部的SELECT*部分询问,其中fragen_id=url的id


查询字符串上的参数在
$\u GET
数组中传递给PHP

因此,
forum\u forum.php
只需将其拾取并绑定到准备好的语句中的占位符

<?php

$statement = $pdo2->prepare("SELECT * FROM fragen WHERE fragen_id = :id");
// here we put a value into the parameter
$result = $statement->execute([':id' => (int)$_GET['id']]);

while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
        echo '<div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subitel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "<br><br>",'<div class="forum_Bild"><img class="pic" alt="Ein Bild" title="Eine Pflanzen" src="'.$row['fragen_picture'].'">',
        "<br><br>","Bildlink:  ".$row['fragen_picture'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div>';

}
?>


当直接从url向您的DBA获取信息时,请务必小心。不,我只是将数据库与url id进行比较