Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!