我正在尝试将 .csv
文件导入 MySql
这是我的 .csv
文件:
,AI,MD5('AIRT2015'),RRHH,14,,,AION S.A. DE C.V.,
这是我的数据库结构:
当我上传 .csv
文件时,我看到的密码与我的 .csv
文件完全相同...
知道如何加密上传吗?
谢谢
最佳答案
首先,网络上有很多帖子介绍为什么单独使用 MD5 来存储密码不是一个好主意(请参阅此 great answer 或搜索 google 以获取更多信息),但要实现您想要的使用以 md5 哈希为例,您需要在没有 md5()
包装器的 .csv
文件中包含密码,并使用某种脚本语言来解析文件并将值INSERT
到数据库中。例如,如果它是一个 Web 应用程序,您可以按照以下方式使用 PHP
:
<?php
// database variables, change these to your details
$dbhost = 'localhost';
$dbusername = 'username';
$dbpassword = 'password';
$dbname = 'database';
$tablename = '`table`';
// file path
$filepath = 'path/to/file.csv';
// connect to the database
$db = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
// get the contents of your file as an array of lines
$lines = file($filepath);
// loop through lines in the array
foreach($lines as $line) {
// split the lines by comma
$split = explode(',', $line);
// we know that the password will be the third element in the array...
// so we hash it and push it back in the same point in the array
$split[2] = md5($split[2]);
// build sql query
$sql = "INSERT INTO {$tablename} (ID_ASOCIADO,USERNAME,PASSWORD,...) VALUES ('" . implode("','", $split) . "')";
// run query and put result into variable
$result = mysqli_query($db, $sql);
if ($result === TRUE) {
echo "RECORD CREATED SUCCESSFULLY => " . $line . PHP_EOL;
} else {
echo "INSERT FAILED WITH ERROR => " . mysql_error($db);
}
}
有关使用 php
和 mysqli
的更多信息,请参阅 here 。它们有大量很棒的示例和解释,特别是在双界面和语句页面上。
关于mysql - 如何将带有 md5 列的 csv 导入到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380538/