CSV import
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@545 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
5c7948dd54
commit
e7eb28d32c
|
@ -100,7 +100,7 @@ if (isset($_GET["download"])) {
|
|||
if (isset($_GET["default"])) {
|
||||
$_GET["edit"] = $_GET["default"];
|
||||
}
|
||||
if (isset($_GET["select"]) && $_POST && (!$_POST["delete"] && !$_POST["export"] && !$_POST["save"])) {
|
||||
if (isset($_GET["select"]) && $_POST && (!$_POST["delete"] && !$_POST["export"] && !$_POST["import"] && !$_POST["save"])) {
|
||||
$_GET["edit"] = $_GET["select"];
|
||||
}
|
||||
if (isset($_GET["callf"])) {
|
||||
|
|
|
@ -207,4 +207,7 @@ $translations = array(
|
|||
'Partitions' => 'Oddílů',
|
||||
'Partition name' => 'Název oddílu',
|
||||
'Values' => 'Hodnoty',
|
||||
'%d row(s) has been imported.' => array('Byl importován %d záznam.', 'Byly importovány %d záznamy.', 'Bylo importováno %d záznamů.'),
|
||||
'CSV Import' => 'Import CSV',
|
||||
'Import' => 'Import',
|
||||
);
|
||||
|
|
|
@ -6,4 +6,5 @@ $translations = array(
|
|||
'%d process(es) has been killed.' => array('%d process has been killed.', '%d processes have been killed.'),
|
||||
'%d row(s)' => array('%d row', '%d rows'),
|
||||
'%d item(s) have been affected.' => array('%d item have been affected.', '%d items have been affected.'),
|
||||
'%d row(s) has been imported.' => array('%d row has been imported.', '%d rows has been imported.'),
|
||||
);
|
||||
|
|
|
@ -80,6 +80,21 @@ if ($_POST && !$error) {
|
|||
}
|
||||
exit;
|
||||
}
|
||||
if ($_POST["import"]) {
|
||||
$file = preg_replace("~^\xEF\xBB\xBF~", '', get_file("csv_file")); //! character set
|
||||
$rows = array(); //! packet size
|
||||
preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches);
|
||||
foreach ($matches[0] as $val) {
|
||||
$row = array();
|
||||
preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $fields);
|
||||
foreach ($fields[1] as $field) {
|
||||
$row[] = "'" . $mysql->escape_string(str_replace('""', '"', preg_replace('~".*"~s', '', $field))) . "'"; //! NULL
|
||||
}
|
||||
$rows[] = "(" . implode(", ", $row) . ")";
|
||||
}
|
||||
$result = queries("INSERT INTO " . idf_escape($_GET["select"]) . " VALUES " . implode(", ", $rows));
|
||||
query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) has been imported.', $mysql->affected_rows), $result, false, !$result);
|
||||
} else {
|
||||
$result = true;
|
||||
$affected = 0;
|
||||
$command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]);
|
||||
|
@ -113,6 +128,7 @@ if ($_POST && !$error) {
|
|||
query_redirect(queries(), remove_from_uri("page"), lang('%d item(s) have been affected.', $affected), $result, false, !$result);
|
||||
//! display edit page in case of an error
|
||||
}
|
||||
}
|
||||
page_header(lang('Select') . ": " . htmlspecialchars($_GET["select"]), $error);
|
||||
|
||||
if (isset($rights["insert"])) {
|
||||
|
@ -226,6 +242,7 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
|||
if (!$result) {
|
||||
echo "<p class='error'>" . htmlspecialchars($mysql->error) . "</p>\n";
|
||||
} else {
|
||||
echo "<form action='' method='post' enctype='multipart/form-data'>\n";
|
||||
if (!$result->num_rows) {
|
||||
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
|
||||
} else {
|
||||
|
@ -236,7 +253,6 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
|||
}
|
||||
}
|
||||
|
||||
echo "<form action='' method='post'>\n";
|
||||
echo "<table border='1' cellspacing='0' cellpadding='2' class='nowrap'>\n";
|
||||
for ($j=0; $row = $result->fetch_assoc(); $j++) {
|
||||
if (!$j) {
|
||||
|
@ -301,9 +317,10 @@ for (var i=0; <?php echo $i; ?> > i; i++) {
|
|||
echo " (" . lang('%d row(s)', $found_rows) . ")</p>\n";
|
||||
|
||||
echo ($_GET["db"] != "information_schema" ? "<fieldset><legend>" . lang('Edit') . "</legend><div><input type='submit' value='" . lang('Edit') . "' /> <input type='submit' name='clone' value='" . lang('Clone') . "' /> <input type='submit' name='delete' value='" . lang('Delete') . "'$confirm /></div></fieldset>\n" : "");
|
||||
echo "<fieldset><legend>" . lang('Export') . "</legend><div><input type='hidden' name='token' value='$token' />$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\n";
|
||||
echo "</form>\n";
|
||||
echo "<fieldset><legend>" . lang('Export') . "</legend><div>$dump_options <input type='submit' name='export' value='" . lang('Export') . "' /></div></fieldset>\n";
|
||||
}
|
||||
$result->free();
|
||||
echo "<fieldset><legend>" . lang('CSV Import') . "</legend><div><input type='hidden' name='token' value='$token' /><input type='file' name='csv_file' /> <input type='submit' name='import' value='" . lang('Import') . "' /></div></fieldset>\n";
|
||||
echo "</form>\n";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue