Currently, I am trying to build a reporting dashboard but before doing so I need to import data from a financial system to a database and change some rows so that the grouping/mapping works with the BI tool.
So my current challenge is:
I can display each CSV file in a dynamic HTML table separated by "th" and "td".
For the "th" I created a "form" so that the user can change the header -> all good and the mapping with the BI tool works as well.
My next step is to create a new table in the database based on the dynamic HTML table I see in the web browser:
F.ex.:
An easy button that does the following:
- create a new table e.g. raw data.
- insert the data from the HTML table to the database base
$raw_data = 'raw data'
$sql_create_table = CREATE table $raw_data (dynamic from the html "th" table)
$sql_insert_data = INSERT INTO $raw_data (dynamic from the html "th" table) VALUES ('dynamic data from the html "td" table')
But the issue is that each time I click the "button" I get some warnings and a fatal error :(
Warning: Undefined array key "file" in C:\xampp\htdocs\meinerstesprojekt\includes\Show_file.php on line 43 Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\meinerstesprojekt\includes\Show_file.php on line 43 Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\meinerstesprojekt\includes\Show_file.php:45 Stack trace: #0 C:\xampp\htdocs\meinerstesprojekt\includes\Show_file.php(45): fopen('', 'r') #1 {main} thrown in C:\xampp\htdocs\meinerstesprojekt\includes\Show_file.php on line 45
<?php
// connect to database
include_once '../includes/dbh.php';
// check connection -> variable inside of include/dbh.php
$title = 'Upload Data';
?>
<!DOCtype html>
<html lang="en">
<head>
<title> <?php echo $title; ?></title>
<link href="../Style/main.css" type="text/css" rel="stylesheet">
<meta charset = "UTF-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1">
</head>
<body class="body">
<button class= btn_insert onclick="history.go(-1);">Go Back</button>
<br>
<?php
// to select the header below
$Accounting_Period = 'Accounting Period';
$Account = 'Account';
$Amount = 'Amount';
$Journal_Entry_Number = 'Journal Entry Number';
?>
<?php
//upload Data
if (isset($_POST["Show"]))
echo '<table id="customers">';
$file_name = $_FILES["file"]["tmp_name"];
$csv_file = fopen("$file_name", "r");
if ($csv_file!==FALSE) {
// show only the first line (header line)
$show_header = fgetcsv($csv_file, 0, ";");
// Counts all columns to create the table header (th)
$column_count_header = count($show_header);
echo '<tr>';
// define the table columns
for ($column_start_header = 0; $column_start_header < $column_count_header; $column_start_header++) {
// add the table header
?>
<th><form method="POST">
<label>
<select>
<option value="<?php echo $show_header[$column_start_header];?>"><?php echo $show_header[$column_start_header];?></option>
<option value="<?php echo $Accounting_Period ?>">Accounting Period</option>
<option value="<?php echo $Account?>">Account</option>
<option value="<?php echo $Amount?>">Amount</option>
<option value="<?php echo $Journal_Entry_Number?>">Journal Entry Number</option>
</select>
</label>
</form>
</th>
<?php
}
echo '</tr>';
}
// while to add all date below the header
while (($show_data = fgetcsv($csv_file, 0, ";")) !== FALSE) {
// Counts all columns to create the table data (td)
$column_count = count($show_data);
// starting table data (td)
echo '<tr>';
// define the columns
for ($column_start = 0; $column_start < $column_count; $column_start++) {
// add the table data
echo "<td>" . $show_data[$column_start] . "</td>";
}
echo '</tr>';
// ending table data
}
fclose($csv_file);
echo '</table>';
// insert data
// create a new table in the database using the header
?>
<form action="" method="post" name="Import CSV">
<button type="submit" name="Import_CSV" class="btn_insert">Import new data</button>
</form>
<?php
$table_name= 'new_person';
if(isset($_POST["Import_CSV"]))
?>
</body>
</html>