在我的主页上,我有一个搜索栏,在页面顶部有一个按钮,我用数据库中的歌曲标题显示了我所有的歌曲.

搜索栏工作正常,因为我输入的每首歌的标题都会把我带到正确的详细信息页面.

我只是想知道我怎样才能点击歌曲标题,并带我到每首歌曲的详细信息页面.

主页

<?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>";
} ?>

详细信息页面

//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>

推荐答案

您可以使用get HTTP方法将歌曲的id发送到details.php页,并根据该id查询数据库.

使用GET HTTP方法搜索操作始终是一个很好的实践.正如mickmackusa在 comments 中所说:

$\u POST在"写入"服务器端数据时最合适$_得到是

因此,将主页上的代码更改如下:

<?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>";
    } 
?>

并更改detail.php,如下所示:

<?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>

使用LIKE搜索标题也是一个好主意,如下所示:

if (!empty($_POST['search'])) {
    $condition = "title LIKE :value";
    $value = "%" . $_POST['search'] . "%";
}

Php相关问答推荐

如何隐藏x轴图表上的值

PHP日期操作,将多个日期输出为格式化字符串

在PHP中循环访问多维数组,并按组显示内容

如何将隐式乘法添加到PHPPEG基本计算器语法中?

来自POST请求的(无效)json字符串中的奇怪字符(编码问题)

基于服务器的不同地平线配置

从 WooCommerce 购物车中删除总计部分,同时保留小计行

将一个表中的行连接为不同表MYSQL中的一列

奇怪的 preg_match_all() 行为

在 DateTime 或 DateTimeImmutable 对象上调用modify() 时是否必须使用空格?

计算数组中连续值的数量

防止使用woocommerce_checkout_process下单

在WooCommerce购物车和 checkout 页面中更改总计文本

Woocommerce单个添加到购物车页面中的产品动态定价

为什么 8.0 之前的 PHP 版本拒绝命名空间Random\Namespace作为无效语法?

将变量从中间件传递到控制器 Laravel

无法在 Laravel 10 中安装 jenssegers/mongodb

Php 的新WeakMap - 枚举曾经被垃圾收集过吗?

如果 static:: 被解析为B,为什么 PHP 会执行A的方法

在 DomPDF 和 Laravel 中使用内联 css