Admin page with SQL table creation and insertion
diff --git a/admin/createtable.php b/admin/createtable.php
new file mode 100644
index 0000000..027788c
--- /dev/null
+++ b/admin/createtable.php
@@ -0,0 +1,19 @@
+<?php
+ require '../credentials.php';
+ require '../php/utils.php';
+
+ $dbname = $_GET['dbname'];
+
+ $create = "CREATE TABLE `".$dbname."` (
+ `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ `nom` varchar(100) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL,
+ `curs` tinyint(1) NOT NULL,
+ `grau` tinyint(1) NOT NULL,
+ `quimata` int(11) NOT NULL,
+ `requested` tinyint(1) NOT NULL DEFAULT 0,
+ `mort` tinyint(1) NOT NULL DEFAULT 0,
+ `password` varchar(100) NOT NULL DEFAULT ''
+ )";
+
+ if (query($create)) header("Location: ./index.php?dbname=".$dbname);
+?>
diff --git a/admin/index.php b/admin/index.php
new file mode 100644
index 0000000..91e742b
--- /dev/null
+++ b/admin/index.php
@@ -0,0 +1,11 @@
+<h1>Crea nova taula/Inserta valors</h1>
+<form action="createtable.php" method="GET">
+ <label>Crear taula: <input type="text" name="dbname" placeholder="Nom de la base de dades" /></label>
+ <input type="submit" />
+</form>
+
+<form action="insert.php" method="POST" enctype="multipart/form-data">
+ <label>Insert CSV: <input type="file" name="csvname" /></label>
+ <input type="text" name="dbname" placeholder="Nom de la base de dades" value="<?=isset($_GET['dbname']) ? $_GET['dbname'] : ''?>" />
+ <input type="submit" />
+</form>
diff --git a/admin/insert.php b/admin/insert.php
index 77df79b..f6f836f 100644
--- a/admin/insert.php
+++ b/admin/insert.php
@@ -1,9 +1,12 @@
<?php
require '../credentials.php';
require '../php/utils.php';
+
+ $csvname = $_FILES['csvname']['tmp_name'];
+ $dbname = $_POST['dbname'];
// Read from CSV
- $inscrits = array_map('str_getcsv', file('inscrits.csv'));
+ $inscrits = array_map('str_getcsv', file($csvname));
array_shift($inscrits); // remove header
$start = 1;
@@ -11,8 +14,9 @@
foreach ($inscrits as $user) {
$i = ($i + 1) % ($start + count($inscrits));
if ($i == 0) $i = $i + 1;
- $template = "INSERT INTO `newusers` (`id`, `nom`, `curs`, `grau`, `quimata`, `requested`, `mort`, `password`)" .
+ $template = "INSERT INTO `".$dbname."` (`id`, `nom`, `curs`, `grau`, `quimata`, `requested`, `mort`, `password`)" .
" VALUES (NULL, '".$user[0]."', '".$user[1]."', '".$user[2]."', ".$i.", '0', '0', '')";
- if (query($template)) echo $template . "<br />";
+
+ if (query($template)) header("Location: ./index.php");
}
?>
diff --git a/php/utils.php b/php/utils.php
index 9a283c3..19b9146 100644
--- a/php/utils.php
+++ b/php/utils.php
@@ -20,7 +20,7 @@
$conn->set_charset("utf8");
// Execute query and save result
- $result = $conn->query($query);
+ if (!$result = $conn->query($query)) echo $conn->error;
// Close the connection
$conn->close();