简体   繁体   中英

PHP MYSQL how to show and list all records from two tables using current table's record id?

I have two tables "naujienos" and "apzvalgos" in my SQL Database. I managed to join them both and get all records from both tables using UNION and ORDER BY timestamp. What i need to do is to match records id by current id to show particular record on webpage.

Here is my code:

<?php
$sql="Select * from `naujienos` UNION Select * from `apzvalgos` order by `timestamp` DESC";
$result=mysqli_query($con,$sql);
if($result){
    while($row=mysqli_fetch_assoc($result)){
        $id=$row['topic_id'];
        $topic_image=$row['topic_image'];
        $topic_name=$row['topic_name'];
        $topic_desc=$row['topic_desc'];
        $timeStamp=$row['timeStamp'];
        $kategorija=$row['kategorija'];
        echo '<div class="card-group pl-5 col-md-3 col-sm-6 mb-5 pb-5">
        <div class="card">
            <img class="card-img-top paveiksliukas" src='. $topic_image.' alt="Card image cap">
            <div class="card-body">
                <h5 class="card-title mb-5">'. $topic_name.'</h4>
                <p class="card-text">'.substr($topic_desc,0,200).'</p>
                <a href="naujienos.php?naujienos_id='. $id .' " class="btn btn-primary">Skaityti toliau</a>
            </div>
            <div class="text-center">
            <b>Kategorija: '. $kategorija. '</b>
            </div>
            <div class="text-center pt-1 pb-1">
            <b>Įkelta: '. $timeStamp. '</b>
            </div>
        </div>
    </div>';
    }
}

?>

Here is how it looks on webpage (its basically all records from two tables and whenever i click on button i go to that particular record):

网页图片

This is the code line where when i click on button i get to that particular record by id:

<a href="naujienos.php?naujienos_id='. $id .' " class="btn btn-primary">Skaityti toliau</a>

My problem is that when i click on "apzvalgos" table button it sends me to "naujienos" table record, because ive written this:

<a href="naujienos.php?naujienos_id='. $id .' " class="btn btn-primary">Skaityti toliau</a>

Right now its only working with "naujienos" records and when i click on "apzvalgos" record it shows me "naujienos" record instead. Both tables have identical columns.

How can i change the code that when i click on records from "naujienos" and "apzvalgos" table's it shows current record from those two tables?

I've tried to change the code line

<a href="naujienos.php?naujienos_id='. $id .' " class="btn btn-primary">Skaityti toliau</a>

to somehow get current table record's id, but i have no idea how to do it.

Can someone help me to fix this problem?

     <a href="naujienos.php?naujienos_id='.$id.' " class="btn btn-primary">Skaityti toliau</a>

     //on naujienos.php file
      $id1 = REQUEST['naujienos_id'];


     //Show record

      $sql = "SELECT * FROM naujienos INNER JOIN apzvalgos ON naujienos.naujienos_id=apzvalgos.apzvalgos_id WHERE naujienos_id='$id1'";
      //your code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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