如何在 php 中正确传递页面 ID

How to pass page Id correctly in php

在我的 AllSong.php 页面中,我显示了我从 MySQL 数据库中获得的所有歌曲标题。当用户单击特定标题时,它将转到该歌曲的详细信息页面。

目前可以导航到详情页面,但是url中显示的歌曲ID不正确。由于某种原因,Id 始终为 13,因此非常感谢任何建议或帮助。

所以现在,当它导航到 Details.php 页面时,我会遇到这样的情况

http://localhost:8888/page/details.php?id=13

而不是他们正确的 id

Allsong.php

<?php
require_once '../config.php';
$sql = "SELECT * FROM song ORDER BY title ASC";
$stmt = $conn->prepare($sql);
$stmt->execute();

// fetch all rows
$songs = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

                    <form action="details.php?id=<?= $song['id'] ?>" method="post" hidden>
                        <input type="text" name="search" id="search2"
                            class="form-control form-control-lg rounded-0 border-primary width =250px;"
                            placeholder="Search..." autocomplete="off" required="">
                        <input type="submit" id='searchclick' name="submit" value="Search"
                            class="btn btn-primary rounded-right">
                    </form>

我的详情页

<?php
require_once '../config.php';
if (isset($_POST['submit'])) {
  $title = $_POST['search'];
  $sql = 'SELECT * FROM song WHERE title = :title';
  $stmt = $conn->prepare($sql);
  $stmt->execute(['title' => $title]);
  $row = $stmt->fetch();

} elseif (!empty($_REQUEST['id'])) {
  $sql = 'SELECT * FROM song WHERE id = :id';
  $stmt = $conn->prepare($sql);
  $stmt->execute(['id' => $_REQUEST['id']]);
  $row = $stmt->fetch();
} else {
  header('location: .');
  exit();
}
?>

输出

Array ( [0] => Array ( [id] => 4 [number] => 3 [title] => I hngilh lo na dawatnak in [chord] => Db [lyrics] => C G Am F When I find myself in times of trouble, Mother Mary comes to me C G F C/E Dm C Speaking words of wisdom, let it be C G Am F And in my hour of darkness, She is standing right in front of me C G F C/E Dm C Speaking words of wisdom, let it be Am G F C Let it be, let it be, let it be, let it be G F C/E Dm C Whisper words of wisdom, let it be [pdf] => PDF-61d2bb72052708.42326233.pdf [ppt] => PPTX-61d2bb720551c1.67609911.pptx [pro] => PRO-61d2bb72058393.63836622.pptx [count] => 18 ) [1] => Array ( [id] => 18 [number] => 25 [title] => 1000 [chord] => G [lyrics] => This is just testing text [pdf] => PDF-623c0127de8981.90516888.pdf [ppt] => PPTX-623c0127deb4d2.47792226.pptx [pro] => PRO-623c0127ded5e3.52791215.pro [count] => 0 ) [2] => Array ( [id] => 3 [number] => 4 [title] => Bawipa Thangthat [chord] => G [lyrics] => {key:C} Verse 1 [G2] You said there will [D/F#]be a da[Em]y When the captives [D]all go free [Asus/C#] And every [Bm7]war will cease [G2] You said there will [D/F#]come a ti[Em]me When the nations of the world will [D]change their minds And be [Asus/C#]led by a [Bm7]little child [pdf] => PDF-61d2bbe5467a42.53452418.pdf [ppt] => PPTX-61d2bbe546a234.50867723.pptx [pro] => PRO-61d2bbe546c3b7.71654144.pptx [count] => 0 ) 

更新 Allsong.php,您必须使用 foreach 才能为每首歌曲更改 id-s。

    <?php
    require_once '../config.php';
    $sql = "SELECT * FROM song ORDER BY title ASC";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    
    // fetch all rows
    $songs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    ?>

<?php
foreach ($songs as $song) { 
        foreach ($song as $s) {
    ?>
        <form action="details.php?id=<?= $s['id'] ?>" method="post" hidden>
            <input type="text" name="search" id="search2"
                class="form-control form-control-lg rounded-0 border-primary width =250px;"
                placeholder="Search..." autocomplete="off" required="">
            <input type="submit" id='searchclick' name="submit" value="Search"
                class="btn btn-primary rounded-right">
        </form>
<?php }} ?>