【问题标题】:Showing the right records显示正确的记录
【发布时间】:2018-09-08 01:36:51
【问题描述】:

我正在为自己创建一个论坛,以了解一切如何运作。它在 PHP (PDO) 中构建,现在我遇到了问题。我不知道如何确保当有人点击board 中的记录时,它只会显示设置为boardtopic_id 的项目

这是主题数据库

这是董事会数据库

代码也显示板:

<section class="col-md-8 connectedSortable">
    <div class="box box-info">
    <div class="panel panel-default">
    <div class="panel-heading main-color-bg">
      <h3 class="panel-title">Boards</h3>
     </div>
     <div class="panel-body">
        <?php
           $boardss = $app->get_boards();
             foreach($boardss as $board){
               echo '<div class="col-md-6">';
               echo '<div class="well dash-box">';
               echo '<h3>'.$board['topic'].'</h3><br>'; 
               echo '<a href="https://####/boards/topics">'  .$board['omschrijving'].'</a>';
               echo '</div>';
               echo '</div>';
            }
        ?>
   </div>
   </div>
</div><!-- /.box -->
</section><!-- right col -->

我使用的函数:

public function get_boards(){
    $getBoards = $this->database->query("SELECT * FROM boards ORDER BY id DESC");
    $boards = $this->database->resultset();

    return $boards;

}


public function get_topics(){
    $getTopic = $this->database->query("
    SELECT topics.*, klanten.foto, klanten.voornaam, klanten.achternaam FROM topics 
    LEFT JOIN klanten ON topics.klant_id=klanten.id
    ORDER BY id ASC");
    $topics = $this->database->resultset();

    return $topics;

}

【问题讨论】:

  • klanten 表数据在哪里?
  • @D-Shih 不需要解决这个问题吗?
  • 将参数$board_id 添加到get_topics(),然后为传递的$board_id 添加WHERE clause,以便仅获取该板的主题。
  • 你能在答案中设置它吗? @KarloKokkak

标签: php html mysql sql where


【解决方案1】:

将参数$board_id 添加到get_topics(),然后为传递的$board_id 添加WHERE 子句,以便仅获取该板的主题。

更新功能:

public function get_topics($board_id){
    $getTopic = $this->database->query("
    SELECT topics.*, klanten.foto, klanten.voornaam, klanten.achternaam FROM topics 
    LEFT JOIN klanten ON topics.klant_id=klanten.id
    WHERE topics.board_id = '$board_id'
    ORDER BY id ASC");
    $topics = $this->database->resultset();
    return $topics;
}

【讨论】:

  • 您可以更改我的代码以使其更清晰吗?
【解决方案2】:

在生成板子页面的链接时,需要指定实际调用的是哪个板子。您当前的代码如下所示:

echo '<a href="https://####/boards/topics">' . $board['omschrijving'] . '</a>

如您所见,href 属性中的调用本身不包含任何信息。相反,您应该在此处添加板的主键:

echo '<a href="https://####/boards/topics?board=' . $board['id'] . '">'  . $board['omschrijving'].'</a>

如果这样做了,您可以通过$_GET在板页面上获取当前板:

$currentBoard = $_GET['board'];
if (!is_numeric($currentBoard)) {
    die('Not a valid board id');
}
$currentBoard = (int)$currentBoard;

有了这些信息,您就可以指定板的主题,方法是将其添加到get_topics() 中的查询中,并将板作为参数添加到函数中。 (get_topics($currentBoard))

SELECT 
    topics.*, 
    klanten.foto, 
    klanten.voornaam, 
    klanten.achternaam 
FROM 
    topics 
    LEFT JOIN klanten ON topics.klant_id=klanten.id
WHERE
    topics.board_id = :board
ORDER BY id ASC

该语句使用参数:board,您可以将a prepared statement 中的参数替换为变量$currentBoard。在这种情况下,我强烈建议您使用准备好的语句。

【讨论】:

  • @maybeiforgotmypassword 嘿。我刚刚将我的查询合并到您在get_topics() 中已有的查询中。出于安全原因,它使用prepared_statements,您应该与该技术取得联系。如果您想快速尝试一下,只需编写 topics.board_id = '{$currentBoard}' 而不是使用 :board 占位符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
相关资源
最近更新 更多