简体   繁体   English

如何使 sql 查询在数据库表中根据 id 显示 1 个结果

[英]How to make sql query to display 1 result based on id in the database table

I need help, I cannot figure out, I cannot find why I am having errors and I am not able to achieve something freaking simple.我需要帮助,我无法弄清楚,我找不到我为什么会出错并且我无法实现一些非常简单的事情。

Long story short, I have a website to manage projects, so when I run the search function it throws a table with some records from the database, there is a button called "see details" which is assigned to a project id with database ie 21, 1, 48 etc, the problem is that when I click "see details" it throws everything from the table proposals instead of 1 project, no matter which button I click on, if its id 1, 21, 48, it prints everything.长话短说,我有一个网站来管理项目,所以当我运行搜索 function 时,它会从数据库中抛出一个包含一些记录的表,有一个名为“查看详细信息”的按钮,它被分配给一个项目 id 与数据库即 21 , 1, 48 等,问题是当我点击“查看详细信息”时,它会从表格提案中抛出所有内容,而不是 1 个项目,无论我点击哪个按钮,如果它的 id 为 1、21、48,它会打印所有内容。

details page details.php:详细信息页面详细信息。php:

<?php 
         include '../includes/config.php';

                // Check connection
                  if($link === false){
                    die("ERROR: Could not connect. " . mysqli_connect_error());
                  }
                  // Attempt select query execution
                  $sql = "SELECT * FROM proposals_table WHERE id LIKE '_%'";


                  if($result = mysqli_query($link, $sql)){
                      if(mysqli_num_rows($result) > 0){
                          echo "<table class='table table-bordered'>";
                              echo "<tr>";
                                  echo "<th>Organisation</th>";
                                  echo "<th>Project</th>";
                                  echo "<th>Proposal Date</th>";
                                  echo "<th>Date Received</th>";
                                  echo "<th>Notes</th>";
                              echo "</tr>";
                          while($row = mysqli_fetch_array($result)){
                              echo "<tr>";
                                  echo "<td>" . $row['company'] . "</td>";
                                  echo "<td>" . $row['project'] . "</td>";
                                  echo "<td>" . $row['proposal_date'] . "</td>";
                                  echo "<td>" . $row['date_received'] . "</td>";
                                  echo "<td>" . $row['notes'] . "</td>";

                              echo "</tr>";
                          }
                          echo "</table>";
                          // Free result set
                          mysqli_free_result($result);
                      } else{
                          echo "No records matching your query were found.";
                      }
                  } else{
                      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                  }
                  ?>

search/result page proposals.php搜索/结果页面建议。php

      <?php 
         include '../includes/config.php';

                // Check connection
                  if($link === false){
                    die("ERROR: Could not connect. " . mysqli_connect_error());
                  }
                  // Attempt select query execution
                  $sql = "SELECT * FROM proposals_table";
                  if($result = mysqli_query($link, $sql)){
                      if(mysqli_num_rows($result) > 0){
                          echo "<table class='table table-bordered'>";
                              echo "<tr>";
                                  echo "<th>Organisation</th>";
                                  echo "<th>Project</th>";
                                  echo "<th>Proposal Date</th>";
                                  echo "<th>Date Received</th>";
                                  echo "<th>Options</th>";
                              echo "</tr>";
                          while($row = mysqli_fetch_array($result)){
                              echo "<tr>";
                                  echo "<td>" . $row['company'] . "</td>";
                                  echo "<td>" . $row['project'] . "</td>";
                                  echo "<td>" . $row['proposal_date'] . "</td>";
                                  echo "<td>" . $row['date_received'] . "</td>";
                                  echo "<td> <a class='btn btn-primary' href='details.php?id={$row['id']}'>See details</a></td>";
                              echo "</tr>";
                          }
                          echo "</table>";
                          // Free result set
                          mysqli_free_result($result);
                      } else{
                          echo "No records matching your query were found.";
                      }
                  } else{
                      echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                  }
                  ?>

If you want to show only the selected element on your details page then you need to fetch only that selected item from the database.如果您只想在详细信息页面上显示选定的元素,那么您只需要从数据库中获取该选定的项目。

First of all you should separate HTML from PHP.首先,您应该将 HTML 与 PHP 分开。 The best would be to have them in separate files.最好的办法是将它们放在单独的文件中。 In PHP you prepare the data to be displayed and then in HTML you fill in the blanks with PHP values.在 PHP 中准备要显示的数据,然后在 HTML 中用 PHP 值填写空白。

To select a value from MySQL using a given ID you must use prepared statements with parameter binding.对于 select 使用给定 ID 的 MySQL 的值,您必须使用带有参数绑定的准备好的语句。 So if you create your link in this way:因此,如果您以这种方式创建链接:

echo "<td> <a class='btn btn-primary' href='details.php?id=".urlencode($row['id'])."'>See details</a></td>";

You can receive this ID in your details page using $_GET['id'] .您可以使用$_GET['id']在您的详细信息页面中接收此 ID。 You can bind that value to your WHERE clause in SQL.您可以将该值绑定到 SQL 中的 WHERE 子句。

<?php 
include '../includes/config.php';

// Attempt select query execution
$stmt = $link->prepare("SELECT * FROM proposals_table WHERE id=?");
$stmt->bind_param('s', $_GET['id']);
$stmt->execute();
$proposals = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);


if($proposals) {
?>
    <table class='table table-bordered'>
        <tr>
            <th>Organisation</th>
            <th>Project</th>
            <th>Proposal Date</th>
            <th>Date Received</th>
            <th>Notes</th>
        </tr>
    <?php foreach($proposals as $row): ?>
        <tr>
            <td><?=$row['company'] ?></td>
            <td><?=$row['project'] ?></td>
            <td><?=$row['proposal_date'] ?></td>
            <td><?=$row['date_received'] ?></td>
            <td><?=$row['notes'] ?></td>

        </tr>
    <?php endforeach; ?>
    </table>
<?php
} else {
    echo 'No records matching your query were found.';
}

And of course your config.php page should look like this:当然,您的 config.php 页面应该如下所示:

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'user', 'pass', 'db');
$link->set_charset('utf8mb4'); // always set the charset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM