From 94184a8b4098712071719f06f72db0fdc2bba61c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 27 Jul 2015 01:41:53 +0100 Subject: [PATCH] db fix bug --- lib/php/db.php | 5 +++-- lib/php/db/field.php | 10 ++++++---- lib/php/db/table.php | 35 +++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index ba2e264c..634931e7 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -112,10 +112,11 @@ class db { } */ - function exec($sql,$params=null) { + function exec($sql) { try { - $r = $this->conn->exec($sql,$params); + #$r = $this->conn->exec($sql,$params); + $r = $this->conn->exec($sql); /* if ($params === null) { $r = $this->conn->exec($sql,$params); diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 01826a9c..7cca1198 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -113,17 +113,19 @@ class field { } function pdo_type() { + if (func_get_args()>0 and func_get_arg(0) === null) return PDO::PARAM_NULL; if ($this->numeric) return PDO::PARAM_INT; if (strpos($this->type,'bool') !== false ) return PDO::PARAM_BOOL; if (strpos($this->type,'blob') !== false ) return PDO::PARAM_LOB; # not a bug LOB return PDO::PARAM_STR; } - function bindParam($sth,$value) { + function bindParam($sth,$value,$name=null) { # See: http://php.net/manual/en/pdostatement.bindparam.php - $sth->bindParam(':'.$this->name, $value, PDO::PARAM_INT); - $sth->bindParam(':'.$this->name, $value, PDO::PARAM_STR); - $sth->bindParam(':calories', $calories, PDO::PARAM_INT); + if ($name === null) $name = ':'.$this->name; +#debug($this->name . " | $name | $value | $this->type | " . $this->size()."
"); + return $sth->bindParam($name, $value, $this->pdo_type($value), $this->size()); +// NB 27.07.15 return $sth->bindParam($name, $value, $this->pdo_type($value)); } } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index c043cc41..85939380 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -283,8 +283,9 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. function url_list($k='',$v='') { $params = array(); + $fields = ($this->db->p('action') == 'delete') ? array() : $this->fields(); - foreach (array_merge( $this->params, array_keys($this->fields()) ) as $f) { + foreach (array_merge( $this->params, array_keys($fields) ) as $f) { if (@strcmp($this->db->p($f),'')==0) continue; $params[$f] = $this->db->p($f); @@ -832,23 +833,36 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } function insert($hvalues) { - $names = $values = array(); - #var_dump($hvalues); + $fields = $values = array(); +#var_dump($hvalues); foreach ($this->fields() as $name => $field) { if (!isset($hvalues[$name])) continue; if ($field->key and $field->autoincrement()) continue; - $names[] = $field->sql_name(); - #$values[] = $field->quote($hvalues[$name]); + $fields[$field->sql_name()] = $field; $values[] = $hvalues[$name]; } #bye($values); $sql = - 'INSERT INTO '. $this->sql_name() . ' (' . join(',',$names).')' + 'INSERT INTO '. $this->sql_name() . ' (' . join(',',array_keys($fields)).')' #.' VALUES (' . join(',',$values).')' - .' VALUES (' . join(',',ar_map('"?"',$values)) . ')' + .' VALUES (' . join(',',ar_map('":$a"',array_keys($fields))) . ')' ; - bye(array($sql,$values)); + + if (!($query = $this->db->conn->prepare($sql))) { + err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL); + return false; + } + + foreach ($fields as $name => $field) $field->bindParam($query,$hvalues[$name],":$name"); + +#debug(array($sql,$values)); + if (!($execute = $query->execute())) { + err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL); + return false; + } + + return $execute; return $this->db->exec($sql,$values); return $this->db->exec($sql); @@ -899,13 +913,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } foreach ($fields as $name) { - $query->bindParam(":$name", $hvalues[$name]); + $field->bindParam($query,$hvalues[$name],":$name"); } foreach ($keys as $name) { - $query->bindParam(":key_$name", $hvalues[$name]); + $field->bindParam($query,$hvalues[$name],":key_$name"); } #return $sql; + #bye($sql); if (!($execute = $query->execute())) { err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL); return false; -- 2.47.3