php - php中如何传递值

标签 php html sql

在我的主页中,我的页面顶部有一个带有按钮的搜索栏,我使用其下方数据库中的标题显示了所有歌曲。

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

我只是想知道如何才能单击歌曲标题并将我带到每首歌曲的详细信息页面。

主页

<?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 在评论中所说:

$_POST is most appropriate when "writing" data server-side. $_GET is most appropriate when "reading" data server-side.

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

<?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 - php中如何传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71431921/

相关文章:

html - 网页中的对齐问题 - 响应式 CSS

html - WKHtmltoPDF 双页脚

html - Gmail 和 Quora 的 HTML 中的 class 或 id 字段是如何生成的,为什么?

sql - 如何在组中查找重复项 - SQL 2008

PHP - 不显示数据库中的任何内容 -

PHP MCRYPT - RIJNDAEL 128 白色 Mcrypt CBC 给出不正确的输出

mysql - SQLQuery - 对单个表中按第三个值分组的 2 个值的范围内的值进行计数

sql - Mysql错误: #1247 - Reference 'karma' not supported (reference to group function)

javascript - 在测试/表单提交期间跟踪用户进度

php - 同一页面上的 2 个不同模态会引发错误