1

I have been programming a page to allow you to be able to store passwords within a database and I am having an issue where the table is being printed out 3 times but I have no idea why. I wanted the table to be on the far right of the screen and to have a list downwards of passwords that you have entered into the mysql database but it is not working

Here is the code:

<?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
        }
    }
?>      
5
  • Please don't store passwords in plaintext. See: crackstation.net/hashing-security.htm Commented Mar 6, 2016 at 9:18
  • You output a full table inside your iteration loop over the entries. Off course you get one full table for each entry then... Commented Mar 6, 2016 at 9:19
  • In the mysql_connect that Is not my username or my password, however in the table I wanted them to see the passwords. Commented Mar 6, 2016 at 9:19
  • this is only for local though not for public :) Commented Mar 6, 2016 at 9:20
  • Ohhh!!! let me have a look at that! Commented Mar 6, 2016 at 9:20

2 Answers 2

1

Remove the table code outside of for loop and also no need of for loop:-

<?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>

Note:- Now it's time to move towards to mysqli_* or PDO, because mysql_* is a deprecated library now.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not correctly closing the first php code block and there are errors in the html with unclosed tags ( th ) and missing table row tags.

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

However, the approach above ( which follows your original ) will produce a new table on each iteration through the loop - I suspect you would prefer 1 table with many rows.? Perhaps more like:

<?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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.