143 lines
3.7 KiB
PHP
143 lines
3.7 KiB
PHP
<?php
|
|
$host = '192.168.1.6';
|
|
$db = 'db';
|
|
$user = 'phowell';
|
|
$pass = 'rolley34';
|
|
|
|
$mysqli = new mysqli($host, $user, $pass, $db);
|
|
if ($mysqli->connect_error) {
|
|
die("DB Connection failed: " . $mysqli->connect_error);
|
|
}
|
|
|
|
|
|
// import and get id of welcome session to use as parent
|
|
if (0) {
|
|
$csvFile = 'fa25import.csv';
|
|
$handle = fopen($csvFile, 'r');
|
|
if (!$handle) {
|
|
die("Cannot open CSV file.");
|
|
}
|
|
|
|
$headers = fgetcsv($handle);
|
|
|
|
// Strip BOM from first header field
|
|
if ($headers && substr($headers[0], 0, 3) === "\xEF\xBB\xBF") {
|
|
$headers[0] = substr($headers[0], 3);
|
|
}
|
|
$firstId = null;
|
|
$rowNum = 0;
|
|
|
|
$columns = array_map('trim', $headers);
|
|
$placeholders = array_fill(0, count($columns) + 1, '?'); // +1 for 'parent'
|
|
|
|
echo "Columns:";
|
|
print_r($columns);
|
|
echo "\n\n";
|
|
|
|
$quotedColumns = array_map(fn($col) => "`" . str_replace("`", "``", $col) . "`", $columns);
|
|
$sql = "INSERT INTO conf_sessions (" . implode(",", $quotedColumns) . ", parent) VALUES (" . implode(",", $placeholders) . ")";
|
|
|
|
$stmt = $mysqli->prepare($sql);
|
|
if (!$stmt) {
|
|
die("Prepare failed: " . $mysqli->error);
|
|
}
|
|
|
|
while (($data = fgetcsv($handle)) !== false) {
|
|
// Pad missing fields
|
|
while (count($data) < count($columns)) {
|
|
$data[] = null;
|
|
}
|
|
|
|
$rowNum++;
|
|
$parent = ($rowNum === 1) ? 0 : $firstId;
|
|
|
|
$bindParams = array_merge($data, [$parent]);
|
|
$types = str_repeat('s', count($bindParams)); // All strings; adjust if needed
|
|
|
|
// Create reference array for bind_param
|
|
$refs = [];
|
|
foreach ($bindParams as $key => $value) {
|
|
$refs[$key] = &$bindParams[$key];
|
|
}
|
|
|
|
array_unshift($refs, $types);
|
|
call_user_func_array([$stmt, 'bind_param'], $refs);
|
|
|
|
if (!$stmt->execute()) {
|
|
echo "Error on row $rowNum: " . $stmt->error . "\n";
|
|
continue;
|
|
} else {
|
|
echo "Success for row $rowNum\n";
|
|
}
|
|
|
|
if ($rowNum === 1) {
|
|
$firstId = $mysqli->insert_id;
|
|
}
|
|
}
|
|
|
|
fclose($handle);
|
|
$stmt->close();
|
|
$mysqli->close();
|
|
|
|
echo "Import completed. First ID: $firstId\n";
|
|
|
|
|
|
|
|
|
|
} else {
|
|
// just plain import rows
|
|
$csvFile = 'import2.csv';
|
|
$handle = fopen($csvFile, 'r');
|
|
if (!$handle) {
|
|
die("Cannot open CSV file.");
|
|
}
|
|
|
|
$headers = fgetcsv($handle);
|
|
|
|
// Strip BOM from first header if present
|
|
if ($headers && substr($headers[0], 0, 3) === "\xEF\xBB\xBF") {
|
|
$headers[0] = substr($headers[0], 3);
|
|
}
|
|
|
|
$columns = array_map('trim', $headers);
|
|
$quotedColumns = array_map(fn($col) => "`" . str_replace("`", "``", $col) . "`", $columns);
|
|
$placeholders = array_fill(0, count($columns), '?');
|
|
|
|
$sql = "INSERT INTO conf_sessions (" . implode(",", $quotedColumns) . ") VALUES (" . implode(",", $placeholders) . ")";
|
|
$stmt = $mysqli->prepare($sql);
|
|
if (!$stmt) {
|
|
die("Prepare failed: " . $mysqli->error);
|
|
}
|
|
|
|
$rowNum = 0;
|
|
while (($data = fgetcsv($handle)) !== false) {
|
|
while (count($data) < count($columns)) {
|
|
$data[] = null;
|
|
}
|
|
|
|
$rowNum++;
|
|
$types = str_repeat('s', count($data));
|
|
|
|
$refs = [];
|
|
foreach ($data as $k => $v) {
|
|
$refs[$k] = &$data[$k];
|
|
}
|
|
array_unshift($refs, $types);
|
|
call_user_func_array([$stmt, 'bind_param'], $refs);
|
|
|
|
if (!$stmt->execute()) {
|
|
echo "Error on row $rowNum: " . $stmt->error . "\n";
|
|
} else {
|
|
echo "Inserted row $rowNum\n";
|
|
}
|
|
}
|
|
|
|
fclose($handle);
|
|
$stmt->close();
|
|
$mysqli->close();
|
|
|
|
echo "Import complete.\n";
|
|
|
|
}
|
|
?>
|