Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
Cómo pasar valor en php

En mi página de inicio, tengo una barra de búsqueda con un botón en la parte superior de mi página y debajo mostré todas mis canciones usando su título de mi base de datos.

La barra de búsqueda funciona bien ya que cada título de canción que escribí me llevó a la página de detalles correcta.

Me pregunto cómo puedo hacer clic en el título de la canción y llevarme a la página de detalles de cada canción.

página de inicio

 <?php require_once '../config.php'; $sql = 'SELECT title FROM song ORDER BY title ASC;'; $stmt = $conn->prepare($sql); $stmt->execute(['title' => $title]); // fetch all rows $songTitle = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> //Search bar <form action="chord/details.php" method="post" class="p-3"> <div class="input-group"> <input type="text" name="search" id="search" class="form-control form-control-lg rounded-0 border-primary width =250px;" placeholder="Search..." autocomplete="off" required> <div class="input-group-append"> <input type="submit" name="submit" value="Search" class="btn btn-primary rounded-right"> </div> </div> </form> // Here I display all my songs from the database using their title <?php foreach ($songTitle as $song) { // I'm not sure how to modify here. echo "<a href='chord/details.php'>{$song['title']} <br> </a>"; } ?>

Página de detalles

 //This is working fine with Search Bar <?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(); } else { header('location: .'); exit(); } ?> //Display the song lyrics here <div>Original Key: <?= ucfirst($row['chord']) ?></div><br> <pre data-key=<?= ucfirst($row['chord']) ?> id="pre"> <?= ucfirst($row['lyrics']) ?> </pre>
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Puede usar el método get HTTP para enviar la identificación de la canción a la página de details.php y consultar la base de datos en esa identificación.

Y siempre es una buena práctica usar el método GET HTTP para buscar acciones. Como dijo mickmackusa en el comentario:

$_POST es más apropiado al "escribir" datos del lado del servidor. $_GET es más apropiado cuando "lee" datos del lado del servidor.

Así que cambie el código en la página de inicio como se muestra a continuación:

 <?php require_once '../config.php'; // query changed to fetch id as well $sql = 'SELECT id , title FROM song ORDER BY title ASC;'; $stmt = $conn->prepare($sql); $stmt->execute(['title' => $title]); // fetch all rows $songTitle = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!-- here we change the method to get --> <form action="chord/details.php" method="get" class="p-3"> <div class="input-group"> <input type="text" name="search" id="search" class="form-control form-control-lg rounded-0 border-primary width =250px;" placeholder="Search..." autocomplete="off" required> <div class="input-group-append"> <input type="submit" name="submit" value="Search" class="btn btn-primary rounded-right"> </div> </div> </form> <?php foreach ($songTitle as $song) { // we add the id to the link echo "<a href='chord/details.php?id={$song['id']}'>{$song['title']} <br> </a>"; } ?>

Y cambie el detail.php como a continuación:

 <?PHP //This is working fine with Search Bar require_once '../config.php'; if (isset($_GET['search']) OR isset($_GET['id'])) { $condition = ""; $value = ""; if (!empty($_GET['id'])) { $condition = "id = :value"; $value = $_GET['id']; } elseif (!empty($_GET['search'])) { $condition = "title = :value"; $value = $_GET['search']; } $sql = 'SELECT * FROM song WHERE ' . $condition; $stmt = $conn->prepare($sql); $stmt->execute(['value' => $value]); $row = $stmt->fetch(); } else { header('location: .'); exit(); } ?> //Display the song lyrics here <div>Original Key: <?= ucfirst($row['chord']) ?></div><br> <pre data-key=<?= ucfirst($row['chord']) ?> id="pre"> <?= ucfirst($row['lyrics']) ?> </pre>

También es una buena idea usar LIKE para buscar en el título como se muestra a continuación:

 if (!empty($_POST['search'])) { $condition = "title LIKE :value"; $value = "%" . $_POST['search'] . "%"; }
over 4 years ago · Santiago Trujillo Denunciar

0

Suponiendo que tiene una columna de id en la tabla de song . Podrías hacer algo como esto:

 <?php require_once '../config.php'; $sql = 'SELECT id, title FROM song ORDER BY title ASC;'; $stmt = $conn->prepare($sql); $stmt->execute(); // fetch all rows $songTitle = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> //Search bar <form action="chord/details.php" method="post" class="p-3"> <div class="input-group"> <input type="text" name="search" id="search" class="form-control form-control-lg rounded-0 border-primary width =250px;" placeholder="Search..." autocomplete="off" required> <div class="input-group-append"> <input type="submit" name="submit" value="Search" class="btn btn-primary rounded-right"> </div> </div> </form> // Here I display all my songs from the database using their title <?php foreach ($songTitle as $song) { // I'm not sure how to modify here. echo "<a href='chord/details.php?id=".$song['id]."'>{$song['title']} <br> </a>"; } ?>

Página de detalles

 //This is working fine with Search Bar <?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(); } ?> //Display the song lyrics here <div>Original Key: <?= ucfirst($row['chord']) ?></div><br> <pre data-key=<?= ucfirst($row['chord']) ?> id="pre"> <?= ucfirst($row['lyrics']) ?> </pre>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda