-2

Im kind of new to html and wanted to do a simple program where i store users login information in an associative array, basically using it as a database. In my code i was able to get the array to store information but it will only store one set of username and password. Heres the code:

    <!DOCTYPE HTML>
<html>
    <head>
        <title>Login Form</title>
    </head>
        <body>
            <form action="process.php" method="POST">
                Username: <input type="text" name="username" id="username" />
                Password: <input type="password" name="password" id="password" />
                <input type="submit" value="Login" name="submit" />

            </form>

        </body>
    </html>




    <?php
$name = $_POST['username'];
$pass = $_POST['password'];

echo "<table border=1>";
foreach($name as $key => $n){
    echo "<tr><td>$name[$key]</td><td>$pass[$key]</td></tr>";
}
echo "</table>";
?>
Sean W
  • 1
  • You do realise that anything you create in memory (like an array) will dissappear each time the script terminates dont you – RiggsFolly Feb 25 '19 at 17:30
  • Also `$name` is actually a scalar variable not an array so that loop works purely by accident – RiggsFolly Feb 25 '19 at 17:31
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Feb 25 '19 at 17:31
  • I know, i was thinking maybe using cookies but ive been trying to implement it to no avail, just need a little advice. – Sean W Feb 25 '19 at 17:32
  • 1
    "basically using it as a database" My advice would be to use an _actual_ database. That is what they're for after all. – Patrick Q Feb 25 '19 at 17:34
  • my mentor wants me to try it this way before using a database – Sean W Feb 25 '19 at 17:38
  • 4
    @SeanW Find a new mentor ASAP. – Patrick Q Feb 25 '19 at 17:40

1 Answers1

0

I highly recommend you don't do that, as that is not really flexible for future use. You are much better off actually using a database.

However, that said, what you are looking for (I think) is input arrays. You can find example here:

            <form action="process.php" method="POST">
                Username: <input type="text" name="username[]" id="username" />
                Password: <input type="password" name="password[]" id="password" />
                <input type="submit" value="Login" name="submit" />

            </form>

This allows you to have multiple usernames and passwords like this:

            <form action="process.php" method="POST">
                Username 1: <input type="text" name="username[]" id="username1" />
                Password 1: <input type="password" name="password[]" id="password1" />
                Username 2: <input type="text" name="username[]" id="username2" />
                Password 2: <input type="password" name="password[]" id="password2" />
                Username 3: <input type="text" name="username[]" id="username3" />
                Password 3: <input type="password" name="password[]" id="password3" />
                Username 4: <input type="text" name="username[]" id="username4" />
                Password 4: <input type="password" name="password[]" id="password4" />
                <input type="submit" value="Login" name="submit" />

            </form>

NOTE: Ids in html must be unique, but names do not have that requirement.

Now, in php you can create a table like so:

    <?php
    $name = $_POST['username'];
    $pass = $_POST['password'];

    echo "<table border=1>";
    foreach($name as $key => $n){
        echo "<tr><td>$name[$key]</td><td>$pass[$key]</td></tr>";
    }
    echo "</table>";
    ?>

This will create a 4 row table.

P.S. What you asked was to make an array of username and passwords and make a table, but judging from your comments it looks like you actually might want to authenticate the user using something like cookies or session variables. Thus, I have added some useful resources if you'd like to learn how to use them.