简体   繁体   English

如何从PHP中的两个表显示数据

[英]how to display data from two tables in php

I have two tables in a database, sight_country and sightseeing . 我有一个数据库,两个表sight_countrysightseeing I am inserting the ID of the country field from the sight_country table to s_country field of the table sightseeing . 我从插入乡间田野的ID sight_countrys_country表的字段sightseeing In php I am showing country field values from sight_country in a CSS drop-down menubar. 在php中,我在CSS下拉菜单栏中显示了sight_country国家/地区字段值。

the code is 该代码是

<li class="menu-item-has-children"><a href="#">Sightseeing</a>
         <ul class="sub-menu">
            <?php

                 $qry_st = "select * from `sight_country` limit 5";
                 $rec_st = mysql_query($qry_st );
                 if( mysql_num_rows($rec_st) > 0)
                     {
                     while($res_st = mysql_fetch_array($rec_st))
                    {
                   echo "<li><a href='sightseeing.php?id=$res_st[id]'>".$res_st['country']."</a></li>";
                  }
                   } 
                    ?> 
          </ul>
        </li>

When click on link of county value then I am showing all sightseeing data from the table sightseeing in php page. 当单击县值的链接时,我将在php页面中显示来自表格观光的所有观光数据。

the code is 该代码是

$sql = "select * from `sightseeing` where `s_country` ='$id'";
$res = mysql_query($sql);
$rec = mysql_fetch_array($res);

the country may have two or more related sightseeing data, so I am displaying sightseeing titles from the sightseeing table in a sidebar menu in my PHP page. 该国家可能有两个或多个相关的观光数据,因此我在PHP页面的侧边栏菜单中显示了观光表中的观光标题。

the code is 该代码是

<ul class="st_lnks">
          <?php

                 $qry_st = "select * from `sightseeing` where s_country = '$id'";

                 $rec_st = mysql_query($qry_st );
                 if( mysql_num_rows($rec_st) > 0)
                     {
                     while($res_st = mysql_fetch_array($rec_st))
                    {
                   echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
                  }
                   } 
                    ?> 
      </ul>

when I click link of stitle I want to show it's related sightseeing data in same page. 当我单击标题的链接时,我想在同一页面中显示其相关的观光数据。 How it can be done? 怎么做?

You could give your second link a second parameter. 您可以为第二个链接添加第二个参数。 Give the first parameter a unique name 给第一个参数一个唯一的名称

<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
    while ($res_st = mysql_fetch_array($rec_st)) {
        echo "<li><a href='sightseeing.php?idCountry=$res_st[id]'>".$res_st['country']."</a></li>";
    }
} 
?> 
</ul>

Then add a second parameter when generating the second link: 然后在生成第二个链接时添加第二个参数:

<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
    while($res_st = mysql_fetch_array($rec_st)) {
        echo "<li><a href='sightseeing.php?idCountry=$idCountry&idSightseeing=res_st['id']'>".$res_st['stitle']."</a></li>";
    }
}
?> 
</ul>

I am assuming that; 我假设是这样;

  • The whole script is on one page (sightseeing.php), which varies depending on any GET variables (variables in the URL). 整个脚本位于一页(sightseeing.php)上,该页取决于任何GET变量(URL中的变量)而有所不同。
  • Originally the page just displays the first menu. 最初,该页面仅显示第一个菜单。 Then when u click a country, you are sent again to sightseeing.php. 然后,当您单击一个国家/地区时,您会再次发送到sightseeing.php。 Now also with ?id=* which shows also a second list, containing the list of sightseeing relevant to the country selected. 现在还带有?id = *,它还显示第二个列表,其中包含与所选国家/地区有关的观光列表。
  • You have a field called 'id' in your sightseeing table that has the unique sightseeing id. 您的观光表中有一个名为“ id”的字段,具有唯一的观光ID。

To now additionally show details of the sightseeing selected (clicked by user); 现在,另外显示所选观光的详细信息(用户单击); Modify the links in the second list. 修改第二个列表中的链接。 rather than: 而不是:

echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";

Write: 写:

echo "<li><a href='sightseeing.php?id=$res_st[s_country]&ss_id=$res_st[id]'>".$res_st['stitle']."</a></li>";

Now when u click one of the links and are sent back to to sightseeing.php you will also have another get variable GET['ss_id'] (which has the id of the sightseeing that you want to view). 现在,当您单击链接中的一个并返回到sightseeing.php时,您还将获得另一个获取变量GET ['ss_id'](其中包含您要查看的观光活动的ID)。 You can use this variable to pull the relevant details of the sightseeing. 您可以使用此变量提取观光的相关细节。

$sightSeeingId = $_GET['ss_id'];
$sql3 = "select * from `sightseeing` where `id` ='$sightSeeingId' LIMIT 1";
$res3 = mysql_query($sql3);
$sightSeeingData = mysql_fetch_array($res3);

check that it has data and print it out 检查它是否有数据并打印出来

if(!$res3) die(mysql_error());              
if(mysql_num_rows($res3) > 0){
echo "Sight Seeing id:" . $sightSeeingData['id'];
}

As a side note you should be aware that mysql_* functions are outdated and your code is vunerable to sql injection, see here; 附带说明一下,您应该知道mysql_ *函数已经过时,并且您的代码对于sql注入是脆弱的,请参见此处;

GET parameters vulnerable to SQL Injection - PHP GET参数容易受到SQL注入-PHP

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

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