在我的 AllSong.php 頁面中,我顯示了來自 MySQL 資料庫的所有歌曲標題。當用戶單擊特定標題時,它將進入該歌曲詳細資訊頁面。
目前它可以導航到詳細資訊頁面,但 url 中顯示的歌曲 ID 不正確。由于某種原因,ID 始終為 13,因此任何建議或幫助將不勝感激。
所以現在,當它導航到 Details.php 頁面時,我會變成這樣
http://localhost:8888/page/details.php?id=13
而不是他們正確的身份證
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 )
uj5u.com熱心網友回復:
更新 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 }} ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459034.html
