This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
forked from deliciousbrains/sqlbuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaxsaveedit.php
128 lines (96 loc) · 3.02 KB
/
ajaxsaveedit.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
SQL Buddy - Web based MySQL administration
http://www.sqlbuddy.com/
ajaxsaveedit.php
- saves data to the database
MIT license
2008 Calvin Lough <http://calv.in>
*/
include "functions.php";
loginCheck();
if (isset($db))
$conn->selectDB($db);
if ($_POST && isset($table)) {
$insertChoice = "";
if (isset($_POST['SB_INSERT_CHOICE'])) {
$insertChoice = $_POST['SB_INSERT_CHOICE'];
}
$structureSql = $conn->describeTable($table);
if ($conn->getAdapter() == "mysql") {
while ($structureRow = $conn->fetchAssoc($structureSql)) {
$pairs[$structureRow['Field']] = '';
$types[$structureRow['Field']] = $structureRow['Type'];
$nulls[$structureRow['Field']] = (isset($structureRow['Null'])) ? $structureRow['Null'] : "YES";
}
} else if ($conn->getAdapter() == "sqlite") {
foreach ($structureRow as $column) {
$pairs[$column[0]] = '';
}
}
foreach ($_POST as $key=>$value) {
if ($key != "SB_INSERT_CHOICE") {
if (is_array($value)) {
$value = implode(",", $value);
}
$pairs[$key] = $conn->escapeString($value);
}
}
if (isset($pairs)) {
if ($insertChoice != "INSERT") {
$updates = "";
foreach ($pairs as $keyname=>$value) {
if ($conn->getAdapter() == "mysql") {
if (isset($types) && substr($value, 0, 2) == "0x" && isset($binaryDTs) && in_array($types[$keyname], $binaryDTs)) {
$updates .= "`" . $keyname . "`=" . $value . ",";
} else if (!$value && !($value != '' && (int)$value == 0) && $nulls[$keyname] == "YES") {
$updates .= "`" . $keyname . "`=NULL,";
} else {
$updates .= "`" . $keyname . "`='" . $value . "',";
}
}
}
$updates = substr($updates, 0, -1);
if (isset($_GET['queryPart']))
$queryPart = $_GET['queryPart'];
else
$queryPart = "";
if ($conn->getAdapter() == "mysql") {
$query = "UPDATE `$table` SET " . $updates . " " . $queryPart;
} else if ($conn->getAdapter() == "sqlite") {
$query = "UPDATE '$table' SET " . $updates . " " . $queryPart;
}
} else {
$columns = "";
$values = "";
foreach ($pairs as $keyname=>$value) {
if ($conn->getAdapter() == "mysql") {
$columns .= "`" . $keyname . "`,";
} else if ($conn->getAdapter() == "sqlite") {
$columns .= "'" . $keyname . "',";
}
if (isset($types) && substr($value, 0, 2) == "0x" && isset($binaryDTs) && in_array($types[$keyname], $binaryDTs)) {
$values .= $value . ",";
} else {
$values .= "'" . $value . "',";
}
}
$columns = substr($columns, 0, -1);
$values = substr($values, 0, -1);
if ($conn->getAdapter() == "mysql") {
$query = "INSERT INTO `$table` ($columns) VALUES ($values)";
} else if ($conn->getAdapter() == "sqlite") {
$query = "INSERT INTO '$table' ($columns) VALUES ($values)";
}
}
$conn->query($query) or ($dbError = $conn->error());
echo "{\n";
echo " \"formupdate\": \"" . $_GET['form'] . "\",\n";
echo " \"errormess\": \"";
if (isset($dbError))
echo $dbError;
echo "\"\n";
echo '}';
}
}
?>