【问题标题】:Carry over specific data from mysql query result when user selects data用户选择数据时从mysql查询结果中结转特定数据
【发布时间】:2019-05-16 14:12:17
【问题描述】:

这是我关于stackover flow的第一个问题,所以希望我能解释清楚。我对 php/js/html 相当陌生,但遇到了问题。我使用会话变量查询我的数据库,它返回与登录用户关联的所有结果。 下面是我用来获取结果的 php 代码。

<?php

session_start();

include('conn.php');

if(!isset($_SESSION['40219357_user'])){
    header('Location:index.php');
}

    $username = $_SESSION['40219357_user'];
    $userid = $_SESSION['40219357_id'];

    $read = "SELECT * FROM medi_users WHERE dr_id = '$userid'";
    $result = $conn ->query($read);

?>

此查询的结果显示在我网站上的表格中。当网站的登录用户点击一个人的记录时,它应该显示与该特定人有关的所有信息。

自从提出我最初的问题后,我通过将用户 ID 作为隐藏值传递到按钮中找到了解决问题的简单方法。代码如下。

<?php

        while($row = $result ->fetch_assoc()){
            $rowid = $row['id'];
            $firstname = $row['firstname'];
            $surname = $row ['surname'];
            $dob = $row['dob'];
            $address = $row['address'];
            $town = $row['town'];
            $postcode = $row['postcode'];


            echo"   
                <tbody>
                    <tr>
                        <td>$firstname</td>
                        <td>$surname </td>
                        <td>$dob</td>
                        <td>$address</td>
                        <td>$town</td>
                        <td>$postcode</td>
                        <td><a class = 'btn btn-danger' 
                 href `='patientsmedication.php?editid=$rowid'>View</a></td>`
                    </tr>

            ";

        }
    ?>
</tbody>
    </table>
    </div>
</div>

我完全理解这样做不是一种非常安全的方法,并且我愿意接受有关如何正确执行此操作的建议,因为我很想学习。

【问题讨论】:

  • 嗨@kmcs87,欢迎来到SO。这是一个有点模糊的问题,可能需要更多代码。根据您的目标,有不同的方法可以在 PHP 中传递信息。您能否添加更多代码,以便我们可以看到您为传递数据所做的尝试,初始查询的输出结果如何 - (例如,您是否在构建表单、带有按钮的表格、链接列表等)。另外,由于您是新手,因此您应该从正确的角度开始,并阅读有关准备好的语句和 PDO 的内容,因为这将保护您的代码免受注入
  • 也试试这个,它可能会给你一些想法:stackoverflow.com/questions/871858/… 不是你应该看帖子并得到答案
  • 我不明白你的问题,请你试着解释一下好吗
  • @jameson2012 感谢您的快速响应,我无法相信人们会如此迅速地提供帮助。感谢您提供有关 PDO 的建议,因为这是我昨天才知道的,我希望能尽快上手,因为我也在学习 java。我已经编辑了我的问题,所以我希望这可以更清楚地说明我希望实现的目标。
  • @LukeDS 感谢您的回复,非常感谢。我已经编辑了我的问题,所以我希望这可以更清楚地说明我希望实现的目标。

标签: php mysql phpmyadmin qsqlquery


【解决方案1】:
<?php

session_start();
//I think you are already using PDO/Mysqli prepared statements
//in this file since you call $conn->query below
include('conn.php');

    if(!isset($_SESSION['40219357_user'])){
        header('Location:index.php');
    }
    //
    $username = $_SESSION['40219357_user'];
    $userid = $_SESSION['40219357_id'];


    //so to make this secure use place markers and bind your params
    $read = "SELECT * FROM medi_users WHERE dr_id = '$userid'";
    //becomes:
    $read = "SELECT * FROM medi_users WHERE dr_id = :userId";
    $q = $conn->prepare($read);
    $q->bindParam(":userId",$userId,PDO::PARAM_INT);
    $q->execute();

    //now you can fetch your result set and store it in a variable
    $results = $q->fetchAll();
?>

然后您可以使用 foreach 循环遍历结果

 echo "<table>
          <tr>
             <th>Heading 1</th><th.....
          </tr>";

  foreach($results as $row) {
        $rowid = $row['id'];
        //I'm not sure if this is the right id,
        //you would need to confirm, I would think you want to have a user id, but obviously don't know the structure
        //of your database - if this is the user (patient?)
        //id then it's fine
        $firstname = $row['firstname'];
        $surname = $row ['surname'];
        $dob = $row['dob'];
        $address = $row['address'];
        $town = $row['town'];
        $postcode = $row['postcode'];

       echo "<tr>
                    <td>$firstname</td>
                    <td>$surname </td>
                    <td>$dob</td>
                    <td>$address</td>
                    <td>$town</td>
                    <td>$postcode</td>
                    <td><a class='btn btn-danger' 
             href='patientsmedication.php?patientId=$rowid'>View</a></td>//or whatever the relevant id is
                </tr>";
    }
    echo "</table">;

我敢肯定,在 url 中传递 id 会有复杂的感觉 - 就我个人而言,我不是一个忠实的粉丝,但我们会在我为只读情况工作的地方这样做,如果你有足够的其他检查,那么 id它自己的对任何人都不是很有用。

现在在 patientsmedication.php 中,您可以使用 $_GET['patientId'] 获取患者 ID

<?php
session_start();   
include('conn.php');

if(!can_view_patient_details()) {
   header('Location:error_page.php');
   exit();
} else {
   $patientId = isset($_GET['patientId'])??0;
   //if you aren't using php7 you won't have the null coalescing operator so use a ternary style like $var = cond ? A : B

   //now do your query

$q = "SELECT * FROM yourtable WHERE patientId = :patientId";
$q = $conn->prepare($q);
$q->bindParam(":patientId",$patientId,PDO::PARAM_INT);
$q->execute();

//now you can fetch your result set and store it in a variable
$results = $q->fetchAll();

}

function can_view_patient_details() {
    //this should return true or false
    //you would need to design your own permissions checks, 
    //given the nature of your project I would think you would
    //do a database call to confirm the user has the right access
    //to the patient, but you may just check that the correct
    //sessions are set, you'd have to decide what is most appropriate
}


?>

然后根据您的结果,您可以创建您认为合适的页面 - 如果您要使用此页面更新详细信息,我建议您使用表单,因为您可以使用 $_POST 方法,该方法不会在url - 然后我建议它通过控制器对权限、数据类型等进行所有正确的检查。

如果您还没有接触过 MVC 模式(如果您刚刚开始,这很可能),那么至少将您的表单指向一个单独的脚本,然后返回此页面并提供一些反馈 - 或者通过url 或通过设置会话消息并将其回显。

值得注意的是,我假设您使用的是 PDO 而不是 Mysqli 准备好的语句,它们都很好,但语法略有不同,我的回答仅在 PDO 中使用 PDO,您不再需要 em> 在你的位置标记上使用分号 (:userId == userId) 但我个人更喜欢它在编写 sql 时的可读性。此外,您的会话名称看起来像是在名称中包含用户 ID(它可能是一个内部代码,尽管这意味着某些东西),但如果它是 id,那么以这种方式设置它的可扩展性不是很高 - 它更简单有一个名为 'user' 的会话并给它 id 的值 - 否则你怎么知道会话的名称而不查找用户,这会破坏对象。

希望这将为您指明正确的方向,我建议您阅读 PDO 和 MVC 模式

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多