-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
54 lines (43 loc) · 1.41 KB
/
server.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
<?php
$post = array_slice($_POST, 0);
# Helpers
$rows_num = (int)$post['rows'];
unset($post['rows']);
$separator = $post['separator'];
unset($post['separator']);
# Some custom data
$hidden_input_names = json_decode($post['hidden_input_names']);
unset($post['hidden_input_names']);
for ($i=0; $i < count($hidden_input_names); $i++) {
unset($post[$hidden_input_names[$i]]);
}
$row_length = count($post) / $rows_num;
# Categories
$categories = get_categories($post, $row_length, $separator);
# Values
$values = get_values($post, $rows_num, $row_length);
$query = 'INSERT into mytable (' . implode(',', $categories) . ') values ' . implode(', ', $values) . ';';
echo $query . "<br>";
function get_categories($post, $row_length, $separator) {
return array_map(function ($item) use ($separator) {
return explode($separator, $item)[0];
}, array_keys(array_slice($post, 0, $row_length)));
}
function get_values($post, $rows_num, $row_length) {
foreach ($post as $key => $value) {
if ((string)((float)($value)) != (string)$value) {
$post[$key] = '\'' . addslashes($value) . '\'';
}
}
$values = [];
for ($i=0; $i < $rows_num; $i++) {
$values []= '(' . implode(',', array_slice($post, $i*$row_length, $row_length)) . ')';
if ($values[$i] === '(' . implode(',', array_fill(0, $row_length, '\'\'')) . ')') {
unset($values[$i]);
}
}
if (!count($values)) {
throw new Exception('there is no data', 1);
}
return $values;
}