I have 500+ user:pass and i want add all data with textarea.
Example ;
user:pass user:pass user:pass
I use the following codes to add users individually ;
JavaScript
x
$username = $_POST['username'];
$password = $_POST['password'];
$query = $db->prepare("INSERT INTO users SET user = ?, pass = ?");
$insert = $query->execute(array($username, $password));
How can I make this to add multiple data user = username : pass = password
Thanks in advance.
Advertisement
Answer
you could use explode
to split your string with a separator for example if you have a string like this
JavaScript
john:1234 mario:465789
after explode
it will be like this
JavaScript
$data = explode(' ', $_POST['textarea']);
print_r($data);
/*
[
'john:1234',
'mario:465789'
]
*/
after that, you could explode
it one more time from the ‘:’ so you could try something like this
JavaScript
$data = $_POST['textarea'];
$data = explode(' ', $data);
foreach($data as $record){
$user = explode(':', $record);
$query = $db->prepare("INSERT INTO users SET user = ?, pass = ?");
$insert = $query->execute(array($user[0], sha1($user[1])));
}
another thing it is so dangerous to save
password
as plain text so I just addsha1
to add some level of security for hashing the password
Another solution
you could also use regex
to be more efficient like this
JavaScript
$data = $_POST['textarea'];
preg_match_all('/(S+:S+)/m', $data, $matches);
foreach($matches as $record){
$user = explode(':', $record);
$query = $db->prepare("INSERT INTO users SET user = ?, pass = ?");
$insert = $query->execute(array($user[0], sha1($user[1])));
}