-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.php
108 lines (102 loc) · 4.03 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
require_once "Utility.php";
$allowed_types = ['int', 'text'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$table = $_POST["table"] ?? null;
checkTable($table, true);
$pk = $_POST["pk"] ?? null;
$field_count = $_POST["field_count"] ?? null;
if (!checkValid($pk) or !checkValid($field_count)) {
die("Campo non valido: $pk");
}
$params = [];
for ($i = 0; $i < $field_count; $i++) {
$param = $_POST["field$i"] ?? null;
$type = $_POST["type$i"] ?? null;
if (checkValid($param) and checkValid($type) and ctype_alpha($param) and in_array($type, $allowed_types)) {
array_push($params, $param, $type);
} else {
die("Campo non valido: $param");
}
}
$sql = "CREATE TABLE $table($pk INT NOT NULL AUTO_INCREMENT PRIMARY KEY";
$params_count = count($params);
if ($params_count / 2 > 0) {
for ($i = 0; $i < $params_count; $i += 2) {
$sql .= ', ' . $params[$i] . ' ' . $params[$i + 1];
}
}
$sql .= ')';
if (query($sql)) {
header("Location: index.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Crea tabella</title>
<?php bootstrap(); ?>
</head>
<body>
<div class="container">
<h1 class="text-primary">Crea tabella</h1>
<form method="POST">
<input class="form-control" type="text" name="table" placeholder="Table Name"><br>
<input class="form-control" type="text" name="pk" placeholder="Primary Key"><br>
<input type="hidden" id="field_count" name="field_count" value="0">
<h2 class="text-primary">Aggiungi campi</h2><br>
<span id="fields">
<template>
<div class="row">
<div class="col-sm-1">
<div class="text-center">
<button class="btn btn-close btn-warning" type="button" onclick="removeField(this)"></button>
</div>
</div>
<div class="col">
<input class="form-control" type="text" name="field{{number}}" placeholder="Nome Campo">
</div>
<div class="col">
<select class="form-control" name="type{{number}}">
<?php
foreach ($allowed_types as $allowed_type)
echo "<option value='$allowed_type'>$allowed_type</option>";
?>
</select>
</div>
</div>
</template>
</span>
<br>
<button type="button" class="btn btn-info" onclick="addField()">Aggiungi campo</button>
<br><br>
<input type="submit" class="btn btn-primary" value="Crea tabella"></button>
</form>
</div>
<script>
var fields = [];
let field = document.getElementsByTagName("template")[0];
const fieldsContainer = document.getElementById("fields");
const fieldCount = document.getElementById("field_count");
function addField() {
let newField = field.content.cloneNode(true);
newField.querySelector("input").name = "field" + fields.length;
newField.querySelector("select").name = "type" + fields.length;
fieldsContainer.appendChild(newField);
fields.push(newField);
fieldCount.value = fields.length;
}
function removeField(button) {
button.parentElement.parentElement.parentElement.remove();
fields.splice(fields.indexOf(button.parentElement.parentElement), 1);
fieldCount.value = fields.length;
}
addField();
</script>
<?php overrideBack() ?>
</body>
</html>