]> git.nbdom.net Git - nb.git/commitdiff
db fix bug
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 27 Jul 2015 00:41:53 +0000 (01:41 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 27 Jul 2015 00:41:53 +0000 (01:41 +0100)
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php

index ba2e264c5481a454be8fdc8a0eaf3af644f7af0e..634931e782ee747d0c836a0d361cdd1cbdacd89e 100644 (file)
@@ -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);
index 01826a9c5ca8809172bcbf0eed8be0623273ce74..7cca11986898e1c779500fc1eda9aeb6b2a20070 100644 (file)
@@ -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()."<br/>");
+    return $sth->bindParam($name, $value, $this->pdo_type($value), $this->size());
+// NB 27.07.15     return $sth->bindParam($name, $value, $this->pdo_type($value));
   }
 
 }
index c043cc416fb153a503b4286e8ba32fef1b213d26..85939380853dec81c790f806e23afe3557a39c69 100644 (file)
@@ -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;