I have an input form in HTML, which is used to import users data into a database (MYSQL) using PHP.
The information in the input must be like this:
[account]
user = {user1}
pwd = {password1}
expdate = 2028-01-01
[account]
user = {user2}
pwd = {password2}
expdate = 2028-01-01
[account]
user = {user3}
pwd = {password3}
expdate = 2028-01-01
Now, the PHP code should be parsing the number of users and each user info and save each user info separately.
My PHP code is:
$users = $_POST["users"];
$users = explode(chr(10) . chr(10), $users);
for ($i = 0; $i < count($users); $i++) {
$users[$i] = explode(" ", $users[$i]);
$username = $users[$i][2];
$pwd = $users[$i][5];
$exp = $users[$i][8];
if (strtotime($exp) === false) {
// ...code
} else {
$expiredate = date("Y-m-d", strtotime($exp));
}
}
When the input is a single user:
[account]
user = {user1}
pwd = {password1}
expdate = 2028-01-01
My output is mangled (here is the var_export()
output):
array (
'username' => '{user1}
pwd',
'pwd' => '=',
'exp' => '2028-01-01'
)
The problem is the exploding -- the pwd
key is attached to the username
value, the pwd
value =
, and it sometimes give a wrong format for the date which prevents being saved.
As a workaround, after putting spaces after each of the input username and password, all data are parsed successfully.
Also, when I enter multiple users at the input form:
[account]
user = {user1}
pwd = {password1}
expdate = 2028-01-01
[account]
user = {user2}
pwd = {password2}
expdate = 2028-01-01
The PHP code only reads the first user and save the information only for the first one, I want them both being saved.
So:
- I want the PHP code to parse all users information to be saved, not only the first user.
- I want the user information get saved without putting space at the end of the user and password lines.
Tried to edit my code and search a lot, was unlucky , don't know what is wrong with the PHP code.