commit c3f7c6d98994c68fffd791a712b8859a08d9fa9b
parent 571a721358462779a96c3263e689239b606209d3
Author: Claudio Alessi <smoppy@gmail.com>
Date: Wed, 3 Jul 2019 10:39:10 +0200
Improve dbupd(): update all fields instead of 1st set found.
Diffstat:
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/bored.php b/bored.php
@@ -202,13 +202,20 @@ function dbins($tbl, $items) {
function dbupd($tbl, $items, $pk = "id") {
$when = [];
- $keys = array_keys($items[0]);
+
+ /* collects longest available set of keys */
+ $keys = [];
+ foreach($items as $item)
+ foreach($item as $k => $v)
+ $keys[$k] = 1;
+ $keys = array_keys($keys);
+
foreach($items as $item) {
$pv = $item[$pk];
foreach($keys as $k) {
if($k == $pk)
continue;
- $v = dbin($item[$k]);
+ $v = isset($item[$k]) ? dbin($item[$k]) : "`${k}`";
if(!isset($when[$k]))
$when[$k] = [];
$when[$k][] = "when $pv then $v";
@@ -450,8 +457,13 @@ function prepare_form() {
$ret[$t]["grp"] = $grp;
/* related info */
$ret[$t]["info"] = [];
- foreach((array)@$_POST[$grp] as $ik => $info)
+ foreach((array)@$_POST[$grp] as $ik => $info) {
+ if(!isset($info[$t])) {
+ /* XXX warn the user: possible fields mismatch */
+ continue;
+ }
$ret[$t]["info"][$ik] = $info[$t];
+ }
}
$ret[$t][$k] = $txt;
}