douqianxian7008 2015-12-20 14:57
浏览 91
已采纳

如何在php中显示来自两个表的数据

I have two tables in a database, sight_country and sightseeing. I am inserting the ID of the country field from the sight_country table to s_country field of the table sightseeing. In php I am showing country field values from sight_country in a CSS drop-down menubar.

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.

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.

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?

  • 写回答

2条回答 默认 最新

  • duanmu5641 2015-12-20 16:15
    关注

    I am assuming that;

    • The whole script is on one page (sightseeing.php), which varies depending on any GET variables (variables in the URL).
    • Originally the page just displays the first menu. Then when u click a country, you are sent again to sightseeing.php. Now also with ?id=* which shows also a second list, containing the list of sightseeing relevant to the country selected.
    • You have a field called 'id' in your sightseeing table that has the unique sightseeing 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). 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;

    GET parameters vulnerable to SQL Injection - PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看