2016-03-06 38 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>