简体   繁体   中英

How can I find 2 elements in an array and return the array in a form in PHP?

I have a little project about databases and arrays where I have a text file database; call it 'database.txt'. I have a page that I wish to run a query ('username' and 'password') from 2 text boxes, and then the submit button is clicked. Code below:

<1--LOGIN PAGE--> <form action="process.php" method="post"> Username: <input type="text" name="customer"> Password: <input type="password" name="password"> <button type="submit">Login</button> </form>

From here, I want to be able to recall an array from the 'database.txt' file, where the server must search the file for an array where the username and password are in the same array (eg. IF PASSWORD and USERNAME are in the same ARRAY, RECALL all the elements in the array ONLY) and then the server recalls all the elements in that array. Something like a customer database pretty much. I want these elements to go into text boxes so that if I want to edit that record (the record being the array), I then type in the box where the particular element of the array is and hit a save button to save the record to the database.txt file

<1--PROCESS PAGE--> <p>Username: <?php echo ''.$username.''; ?></p><br /> <p>Username: <?php echo ''.$username.''; ?></p><br /> <p>Other information: <? echo ''.$name.', '.$address.', <p>Other information: <? echo ''.$name.', '.$address.', '.$phone_number.'' //** this area for other elements in array **// ?></p>

The array format would be the following:

`array(username=>'',password=>'',name=>'',address=>'',phone_number=>'',`

enter code here...,...,...,) (the ... indicating other elements)

I have studied Software Development this year in High school so my knowledge is limited, I do however know the basic operations of HTML and PHP. Also a new user to Stackoverflow...

I'm trying to keep the project to just HTML and PHP, and no SQL please (if thats possible)? Thanks

You can use a text file with comma delimited data to store this. Then you can take advantage of PHP's fgetcsv :

<?php
function findUser($filename, $username, $password) {
    $f = fopen($filename, "r");
    $result = false;
    while ($row = fgetcsv($f)) {
        if ($row[0] == $username && $row[1] == $password) {
            $result = $row;
            break;
        }
    }
    fclose($f);
    return $result;
}

Your text file would look like this inside:

johndoe,swordfish,John Doe,123 Main St.,111-222-3333,...,...
janedoe,secret,Jane Doe,123 Other St.,444-555-6666,...,...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM