如何使用从ID中获取数据的表中的选择数据来显示弹出窗口?

时间:2015-02-04 10:11:20

标签: php mysql

我是PHP和MySQL的初学者。我可以在MySQL数据库的表中显示数据。没关系。但我想要知道如何显示一个包含表中所选数据的弹出窗口。

到目前为止我已经这样做了,但我需要的是当用户点击或选择表格行时,弹出窗口必须出现在表格附近并带有选择数据。

这是我的代码,请告诉我我必须做的事情:

<?php
include('config.php');
?>
<html>
  <head>
    <title>Event Patrol</title>
  </head>
  <body>
    <br />
    <center>
    <?php
    $conn = mysql_connect('I have', 'I have', 'I have');
    mysql_select_db('I have', $conn);

    echo '<table>';
    '<tr>';
    '<td>Event</td>';
    '<td>Record Date</td>';
    '</tr>';
    $sql = "SELECT * from people where PersonId = '".$_GET["PersonId"]."'";
    $rs = mysql_query($sql,$conn) or die(mysql_error());
    while($result= mysql_fetch_array($rs)) {
        echo '<tr>
        <td>'.$result["Event"].'</td>
        <td>'.$result["RecordDate"].'</td>
        </tr>';
    }
    echo '</table>';
    ?>
    </div>
    </center>

    <div id="r" style="width:600px; align:centre"> </div>
    <center>
      <div id="content">
      <?php
      // Open database connection
      $con = mysql_connect("sql14.cpt1.host-h.net","rootaccess1","Screen_1");
      mysql_select_db("sentinelcrm", $con);

      $sql = "SELECT * FROM people";

      $result = mysql_query($sql);
      echo '<table cellpadding="5" cellspacing="0" border="1">';
      echo '<tr>';
      echo '<th>ID</th>';
      echo '<th>Event</th>';
      echo '<th>Control Room</th>';      
      echo '<th>Date</th>';   
      echo '<th>Site ID </th>';
      echo '<th>Action</td>';
      echo '</tr>';
      while ($rows = mysql_fetch_array($result)) {
          echo    '<tr>';
          echo    '<td>';
          echo    $rows['PersonId'];
          echo'</td>';
          echo    '<td>';
          echo $rows['Event'];
          echo '</td>';
          echo    '<td>';
          echo $rows['Control_Room_ID'];
          echo '</td>';
          echo    '<td>';
          echo $rows['RecordDate'];
          echo '</td>';
          echo '<td><a href="" >';
          echo $rows['Site_ID'];
          echo '</a></td>';
          echo '<td><input type="button" value="Edit" id="opener"/>';
          echo  '</tr>';
      }
      ?>
      </div>
    </center>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

您的代码中存在漏洞:

$sql= "SELECT * from people where PersonId = '".$_GET["PersonId"]."'";

由于您尚未检查此用户输入中的内容,黑客可以注入自己的SQL并以您不想要的方式在数据库上运行它。对于这个库,黑客无法运行任何东西,因为它必须在SELECT查询的上下文中有意义,并且(幸运的是)此库不支持一次运行多个查询。这将允许他们发出DELETEDROP或其他具有破坏性的内容。

然而,黑客可以修改SELECT语句,让自己可以访问他们无法访问的帐户或数据。这个特定的查询可能不是安全敏感的,但是,安全性是你应该始终拥有的心态,而不仅仅是当你认为它很重要时。

删除安全漏洞的简单方法是将数字转换为整数(假设此字段确实是整数而不是字符串变量)。它并不理想,但它会起作用:

$personId = (int) $_GET["PersonId"];
$sql= "SELECT * from people where PersonId = '{$personId}'";

现在这是安全的。但是,建议采取两种以下操作:

  • 切换到现代库,例如PDO / mysql或mysqli,因为您的库已被弃用
  • 使用查询参数化(搜索它)