return $db->error;\r
}\r
\r
+ /**\r
+ * Remove columns from array $arr that aren't in table $table\r
+ * \r
+ * @param string $table Table name\r
+ * @param array &$arr Column=>Value array from json (by ref)\r
+ */\r
+ function check_cols($table, &$arr){\r
+ $query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));\r
+ logger("uimport: $query", LOGGER_DEBUG);\r
+ $r = q($query);\r
+ $tcols = array();\r
+ // get a plain array of column names\r
+ foreach($r as $tcol) {\r
+ $tcols[] = $tcol['Field'];\r
+ }\r
+ // remove inexistent columns\r
+ foreach($arr as $icol=>$ival) {\r
+ if (!in_array($icol, $tcols)) {\r
+ unset($arr[$icol]);\r
+ }\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Import data into table $table\r
+ * \r
+ * @param string $table Table name\r
+ * @param array $arr Column=>Value array from json\r
+ */\r
function db_import_assoc($table, $arr){\r
- if (IMPORT_DEBUG) return true;\r
if (isset($arr['id'])) unset($arr['id']);\r
+ check_cols($table, $arr);\r
$cols = implode("`,`", array_map('dbesc', array_keys($arr)));\r
$vals = implode("','", array_map('dbesc', array_values($arr)));\r
$query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";\r
logger("uimport: $query",LOGGER_TRACE);\r
+ if (IMPORT_DEBUG) return true;\r
return q($query);\r
}\r
\r