[英]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.我的 SQL 数据库中有两个表“naujienos”和“apzvalgos”。 I managed to join them both and get all records from both tables using UNION and ORDER BY timestamp.
我设法将它们都加入并使用 UNION 和 ORDER BY 时间戳从两个表中获取所有记录。 What i need to do is to match records id by current id to show particular record on webpage.
我需要做的是通过当前 ID 匹配记录 ID,以在网页上显示特定记录。
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):这是它在网页上的样子(它基本上是两个表中的所有记录,每当我单击按钮 i go 到该特定记录时):
This is the code line where when i click on button i get to that particular record by id:这是代码行,当我单击按钮时,我通过 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:我的问题是,当我点击“apzvalgos”表按钮时,它会将我发送到“naujienos”表记录,因为我写了这个:
<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.现在它只适用于“naujienos”记录,当我点击“apzvalgos”记录时,它会显示“naujienos”记录。 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?我如何更改代码,当我单击“naujienos”和“apzvalgos”表中的记录时,它会显示这两个表中的当前记录?
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.以某种方式获取当前表记录的 ID,但我不知道该怎么做。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.