[英]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'.
称之为“ 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.
我有一个页面,希望从2个文本框中运行查询(“用户名”和“密码”),然后单击“提交”按钮。 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>
<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. 从这里开始,我希望能够从“ database.txt”文件中调出一个数组,服务器必须在该文件中搜索用户名和密码位于同一数组中的数组(例如,如果PASSWORD和USERNAME位于相同的数组,仅调用数组中的所有元素),然后服务器调用该数组中的所有元素。 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
我希望这些元素进入文本框,以便如果我要编辑该记录(记录是数组),然后在该数组的特定元素所在的框中键入内容,然后单击保存按钮以将记录保存到database.txt文件
<1--PROCESS PAGE-->
<p>Username: <?php echo ''.$username.''; ?></p><br />
<1--PROCESS PAGE-->
<p>Username: <?php echo ''.$username.''; ?></p><br />
<p>Username: <?php echo ''.$username.''; ?></p><br />
<p>Other information: <? echo ''.$name.', '.$address.',
<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>
<p>Other information: <? echo ''.$name.', '.$address.',
'.$phone_number.'' //** this area for other elements in array **//
?></p>
'.$phone_number.'' //** this area for other elements in array **//
The array format would be the following: 数组格式如下:
`array(username=>'',password=>'',name=>'',address=>'',phone_number=>'',`
enter code here...,...,...,)
(the ... indicating other elements) enter code here...,...,...,)
(...表示其他元素)
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. 我今年在高中学习了软件开发,所以我的知识有限,但是我知道HTML和PHP的基本操作。 Also a new user to Stackoverflow...
还是Stackoverflow的新用户...
I'm trying to keep the project to just HTML and PHP, and no SQL please (if thats possible)? 我正在尝试将项目保留为HTML和PHP,而不要使用SQL(如果可以的话)? Thanks
谢谢
You can use a text file with comma delimited data to store this. 您可以使用带有逗号分隔数据的文本文件来存储该文件。 Then you can take advantage of PHP's fgetcsv :
然后,您可以利用PHP的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,...,...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.