0

So when i click the link ''What are SMTP, IMAP etc'' it should only show the article about SMTP but instead shows both articles. How do i show each article to their own titles?

right-column:

  <div class="right-column">
      <h3>Latest articles</h3>

      <?php

      $sql = "SELECT title, article_id FROM articles ORDER BY publish_date DESC LIMIT 10";

      $result = $conn->query($sql);
      if ($result->num_rows > 0) {

         while($row = $result->fetch_assoc()) {
        echo ' <a href="article.php?articleID='.$row["article_id"].'">'.$row["title"].'</a> <br><br>';
         }
      }
       ?>
    </div>

article.php:

<div class="article-info">
  <?php
$sql = "SELECT * FROM articles INNER JOIN users ON articles.author = users.user_name ";
$result = $conn->query($sql);

if($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

      if (isset($_GET["articleID"])) {
            $id = $_GET["articleID"];

          if($id == 1){ // should only show Domain article

              echo '<h2>'.$row["title"].'</h2>';
              echo '<p>Created '.$row["publish_date"].' || By '.$row["first_name"].' '.$row["last_name"].' ('.$row["role"].')</p>';
              echo $row["text"];
          } else if($id == 2) { //should only show SMTP article
              echo '<h2>'.$row["title"].'</h2>';
              echo '<p>Created '.$row["publish_date"].' || By '.$row["first_name"].' '.$row["last_name"].' ('.$row["role"].')</p>';
              echo $row["text"];
            }
          }
   }
}
?>
</div>

which looks like this: page after clicking ''What are SMTP, IMAP, POP3''

I tried using the WHERE clause in the sql:

$sql = "SELECT * FROM articles INNER JOIN users ON articles.author = users.user_name WHERE article_id = 1";

but it prints the same article for both links

brombeer
  • 8,716
  • 5
  • 21
  • 27
  • 1
    A [mcve] is a great start when asking for SQL assistance. – jarlh Mar 07 '23 at 11:56
  • 1
    `it prints the same article for both links`...well obviously, because you hard-coded the ID into the query (`WHERE article_id = 1`). Use `WHERE article_id = ?` and then pass `$_GET["articleID"]` into the SQL as a parameter, to use the selected value when restricting the results. See [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) for how to implement that correctly (and safely, and reliably). – ADyson Mar 07 '23 at 12:09

0 Answers0