2016-03-06 54 views
1

我一直在编程一个页面,让您能够在数据库中存储密码,我有一个问题,其中表打印出来3次,但我不知道为什么。我想表是在屏幕最右边的,并有向下的,你已经进入到MySQL数据库的密码列表,但它不工作与MySQL的PHP​​表错误

下面是代码:

<?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 
     } 
    } 
?>  
+0

请不要以明文形式存储密码。请参阅:https://crackstation.net/hashing-security.htm –

+0

您可以在迭代循环内输入一个完整的表格。当然你会得到一个完整的表,每个条目然后... – arkascha

+0

在mysql_connect不是我的用户名或密码,但在表中,我希望他们看到的密码。 – Holy

回答

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_*现在是不推荐使用的图书馆。

0

您没有正确关闭第一个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>