【问题标题】:PHP table error with mysqlmysql的PHP表错误
【发布时间】:2017-04-04 22:53:03
【问题描述】:

我一直在编写一个页面,让您能够在数据库中存储密码,但我遇到了一个问题,即表格被打印了 3 次,但我不知道为什么。我希望该表位于屏幕的最右侧,并在下方列出您已输入 mysql 数据库但无法正常工作的密码

代码如下:

<?php
     $tempUsername = $_SESSION['usernameBox'];
     mysql_connect("localhost", "username", "password");
     mysql_select_db("UserTables");
     $sql = "SELECT * FROM $tempUsername";
     $resultSet = mysql_query($sql);
     while($info = mysql_fetch_array($resultSet)) {
         $counter = count($info);
         for($i = 0; $i < $counter; $i++) {
?>

<table align="right">
     <th> Id </th <th> Website </th> <th> Password </th>
      <tr>
             <td align="center"> <?php echo $info['Id']; ?> </td>
             <td align="center"> <?php echo $info['Website']; ?> </td>
             <td align="center"> <?php echo $info['Password']; ?> </td>
      </tr>
</table>

<?php
        }
    }
?>      

【问题讨论】:

  • 请不要以明文形式存储密码。见:crackstation.net/hashing-security.htm
  • 您在迭代循环中输出一个完整的表来覆盖条目。当然,每个条目你都会得到一张完整的桌子然后......
  • 在 mysql_connect 中不是我的用户名或密码,但是在表中我希望他们看到密码。
  • 这仅适用于本地而非公共 :)
  • 哦!!!让我看看!

标签: php mysql html-table


【解决方案1】:

删除for循环之外的table code,也不需要for循环:-

<?php
     $tempUsername = $_SESSION['usernameBox'];
     mysql_connect("localhost", "username", "password");
     mysql_select_db("UserTables");
     $sql = "SELECT * FROM $tempUsername";
     $resultSet = mysql_query($sql);
?>
    <table align="right">
     <tr><th> Id </th <th> Website </th> <th> Password </th></tr>
<?php
    while($info = mysql_fetch_assoc($resultSet)) { // check that i changed array to assoc and remove for loop because no need
?>
    <tr>
        <td align="center"> <?php echo $info['Id']; ?> </td>
        <td align="center"> <?php echo $info['Website']; ?> </td>
        <td align="center"> <?php echo $info['Password']; ?> </td>
    </tr>
<?php } ?> 

</table>

注意:- 现在是时候改用 mysqli_*PDO,因为 mysql_* 现在是一个已弃用的库。

【讨论】:

    【解决方案2】:

    您没有正确关闭第一个 php 代码块,并且 html 中有未关闭标签 (th) 和缺少表格行标签的错误。

    <?php
         $tempUsername = $_SESSION['usernameBox'];
         mysql_connect("localhost", "username", "password");
         mysql_select_db("UserTables");
         $sql = "SELECT * FROM $tempUsername";
         $resultSet = mysql_query($sql);
         while($info = mysql_fetch_array($resultSet)) {
             $counter = count($info);
             for( $i = 0; $i < $counter; $i++ ) {
    ?>
    
        <table align="right">
            <tr>
                <th> Id </th>
                <th> Website </th>
                <th> Password </th>
            </tr>
            <tr>
                <td align="center"> <?php echo $info['Id']; ?> </td>
                <td align="center"> <?php echo $info['Website']; ?> </td>
                <td align="center"> <?php echo $info['Password']; ?> </td>
            </tr>
        </table>
    
    <?php
            }
        }
    ?> 
    

    但是,上面的方法(遵循您的原始方法)将在循环的每次迭代中生成一个新表 - 我怀疑您更喜欢 1 个包含许多行的表。?也许更像:

    <?php
         $tempUsername = $_SESSION['usernameBox'];
         mysql_connect("localhost", "username", "password");
         mysql_select_db("UserTables");
         $sql = "SELECT * FROM $tempUsername";
         $resultSet = mysql_query($sql);
    ?>
        <table align="right">
            <tr>
                <th> Id </th>
                <th> Website </th>
                <th> Password </th>
            </tr>
    
    <?php
         while( $info = mysql_fetch_array( $resultSet ) ) {
             $counter = count($info);
    ?>
            <tr>
                <td align="center"> <?php echo $info['Id']; ?> </td>
                <td align="center"> <?php echo $info['Website']; ?> </td>
                <td align="center"> <?php echo $info['Password']; ?> </td>
            </tr>
    <?php
        }
    ?> 
    </table>
    

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 2013-08-27
      • 2016-09-02
      • 2012-07-24
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多