doujia9833 2015-04-29 15:32
浏览 14
已采纳

MySQL链接返回结果

I have an image that is displayed from a table and I want the user to be able to click the image which directs them to a page that returns the rest of the data for that row. Do I need a php loop for this? I can't quite figure it out. This returns the Last Name, First Name, and an Image:

    <?php
    if (isset($_GET['LastName'])) {
    $ln = $_GET['LastName'];
    }
        include 'connection.php';
        $query = "SELECT * FROM residents WHERE LastName like '$ln%' ";
        $result = mysql_query($query);


    while($person = mysql_fetch_array($result)) { ?>
    <div class="media col-sm-4">
         <a class="pull-left" href="redirectionpage.php?<?php echo $person['ID'];?>.php">
    <img class="media-object" src="upload/<?php echo $person['Picture'];?>"   width="100" height="100"/>
    </a>
    <div class="media-body">
     <h4 class="media-heading"><?php echo $person['LastName'] . ", " . $person['FirstName']; ?></h4>

   </div>
    </div>
    <?php }>?

Is the best way to accomplish this by redirecting the user to a new page and using a mysql statement to display the new data?

This is the code for the other page:

    <?php
    //Gets data from the database
        include ('header.php');
        include ('footer.php');
        include ('connection.php');



        $query = "SELECT * FROM residents WHERE ID = LastName LIMIT 1";
        $result = mysql_query($query);


       while($row = mysql_fetch_array($result)){
       $outputpicture.='<div><p><img src="upload/' . $row['Picture'].'" width="450" height="550"/></p></div>';
       $outputname.= $row['LastName'] . ", " . $row['FirstName']. '<br />';
       $outputspouse.= $row['Spouse']. '<br />';
       $outputrelatives.= $row['Relatives']. '<br />';
       $outputaddress.= $row['Address']. '<br />';
       $outputbirthday.= $row['Birthday']. '<br />';
       $outputbegan.= $row['BeganResidence']. '<br />';
       $outputended.= $row['EndedResidence']. '<br />';
       $outputformer.= $row['FormerResidence']. '<br />';
       $outputcareer.= $row['Career']. '<br />';
       $outputeducation.= $row['Education']. '<br />';
       $outputmaritalstatus.= $row['MaritalStatus']. '<br />';
       $outputsiblings.= $row['Siblings']. '<br />';
       $outputspecialinterests.= $row['SpecialInterests'].'<br />';
       }


      ?>
  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2015-04-29 16:38
    关注

    The link needs to be changed to

    <a class="pull-left" href="redirectionpage.php?id=<?php echo $person['ID'];?>">
    

    Adding this id= will allow you to access the id parameter in redirectionpage.php via the $_GET superglobal.

    $id = $_GET['id'];
    

    You can use the $id variable to select the specified row from your database. You will see a lot of examples that do it like this:

    $query = "SELECT * FROM residents WHERE ID = $id";
    

    This works but you should not do it, because it creates an SQL injection vulnerability. Read about prepared statements and create your SQL so that you can pass the ID as a paramater.

    $stmt = $pdo->prepare("SELECT * FROM residents WHERE ID = :id");
    $stmt->bindValue(':id', $id, PDO::PARAM_INT);
    $stmt->execute();
    

    This should also be applied to the code you use to select the list of images.

    Assuming ID is the key for your residents table, this should select only one record, so you will not need the while loop. You can just use one fetch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义