Bin4ry
Bin4ry

Reputation: 31

How to explode a line and then explode it again to insert it into a database

Im trying to explode a line where ":" is the first thing to explode and i need to put the 2 pieces into a database after. Then it should read the next line and do the same with that line.

$ex = explode(":", $list);
$array = array($ex);
$ex2   = explode("\r\n", $array);


foreach ($array as $acc)
{
    $data  = "INSERT INTO `accounts` (username, password) VALUES('$acc[0]', '$acc[1]')";
    $query = mysql_query($data);
}

Here is a example of what im trying to do: [email protected]:strokeme1

Make that into: [email protected] strokeme1

Then input the pieces into the Database. Then look on next line and do the same.

[email protected]:strokem221 <-- Explode - Insert

[email protected]:3gir2ls <-- Explode - Insert

[email protected]:eulee1922 <-- Explode - Insert

[email protected]:Abbey82822 <-- Explode - Insert

Upvotes: 0

Views: 204

Answers (1)

David Millar
David Millar

Reputation: 1878

I think your explodes are backwards. First you want to explode $list by '\r\n' to get the individual rows. Then, for each row, explode by ':' to get the pieces of data. I'd also highly highly highly highly recommend not storing passwords as plaintext.

$lines = explode("\r\n", $list);

foreach ($lines as $nextLine) {
    $lineVals = explode(":", $nextLine);
    $lineVals[1] = sha1($lineVals[1]);
    $data  = "INSERT INTO `accounts` (username, password) VALUES('$lineVals[0]', '$lineVals[1]')";
    $query = mysql_query($data);
}

And holy crap I hope that isn't real user account information.

Upvotes: 5

Related Questions