Created
May 12, 2017 06:57
-
-
Save devalexluna/f4209516ebbdc67197ae57afdbf92307 to your computer and use it in GitHub Desktop.
CSV2SQL (PHP & Laravel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title></title> | |
<link rel="stylesheet" href=""> | |
</head> | |
<body> | |
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" accept-charset="utf-8"> | |
<textarea name="accounts"></textarea> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
</body> | |
</html> | |
<?php | |
$mysqli = mysqli_connect('localhost', 'user', 'password', 'database'); | |
mysqli_set_charset($mysqli, "utf8"); | |
if(isset($_POST['submit'])) { | |
$accounts = mysqli_real_escape_string($mysqli, $_POST['accounts']); | |
$data = preg_split('/\s+/', $accounts); | |
foreach ($data as $key => $value) { | |
$split = explode(",", $value); | |
$username = $split[0]; | |
$password = $split[1]; | |
$query = mysqli_query( $mysqli, "INSERT INTO accounts(username, password) VALUES ('$username', '$password')" ); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use DB; | |
public function csv2sql(Request $request) | |
{ | |
// validation | |
$validator = Validator::make($request->all(), [ | |
'csvtext' => 'required|max:22000', | |
]); | |
if ($validator->fails()) { | |
return redirect('users') | |
->withInput() | |
->withErrors($validator); | |
} | |
$csv = $request->csvtext; | |
$data = preg_split('/\s+/', $csv); | |
foreach ($data as $key => $value) { | |
$split = explode(",", $value); | |
$username = $split[0]; | |
$password = $split[1]; | |
DB::table('users')->insert(array('username' => $username, 'password' => $password)); | |
} | |
return redirect('/'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment