Home > Software engineering >  How to pass page Id correctly in php
How to pass page Id correctly in php

Time:04-15

In my AllSong.php page, I displayed all the songs title that I have from MySQL database. When ever user click on a particular title, it'll take to that song detail page.

Currently it can navigate to the details page but how the song Id displaying in the url is not correct. The Id is always 13 for some reason so Any suggestion or help will be really appreciated.

So right now, I'm getting like this when it navigate to Details.php page

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

Instead of their correct 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"
                            
                            placeholder="Search..." autocomplete="off" required="">
                        <input type="submit" id='searchclick' name="submit" value="Search"
                            >
                    </form>

My details page

<?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();
}
?>

Output

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 ) 

CodePudding user response:

Update Allsong.php, you have to use foreach in order for the id-s to change for each song.

    <?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"
                
                placeholder="Search..." autocomplete="off" required="">
            <input type="submit" id='searchclick' name="submit" value="Search"
                >
        </form>
<?php }} ?>
  • Related