/** * Conducts an insert without the need to pass SQL code * * This function will insert a row in the the specified table ($tableName) with values specified by * the array $arUpdates. $arUpdates is associative, where the keys are interpreted as field names. * Field names that appear in the table but not in $arValues will not be specified in the SQL query. * This may cause problems for fields specified as NOT_NULL or similar. If a value of '#id#' is * specified then this will be assumed to be an auto increment primary key field, causing the new * id to be returned. * * @param string $tableName The name of the table to update * @param array $arValues Associative array of fields/values to be inserted * @param bool $prepare Set to false if you do not wish the query to use prepared statements (not recommended) * @return int The next ID if #id# is specified as a value, otherwise the number of affected rows. * */ public function insert($tableName, $arValues, $prepare = true) { $id = null; //echo "<br> called insert: " . $tableName . print_r($arValues); self::logQuery(); $arValueList = array(); $count = 0; foreach ($arValues as $key => &$value) { if (strtolower($value) == '#id#') { //we need to get the next value from this table's sequence $value = $id = $this->conn->nextID($tableName . "_" . $key); //Fix as values are not updating correctly //$id = $this->conn->nextID($tableName . "_id"); //echo "<br>" . $tableName . "_id : " . $value; if (DB::isError($id)) { throw new LoggedException($id->getMessage(), $id->getCode(), self::module); } //array_splice($arValues,$count,1); } // else { //else added as part of fix for value insert $bin = false; if (strtolower(substr($value, 0, 1)) == "b" && strtolower(substr($value, strlen($value) - 1, 1)) == "b") { if (Database::binaryCheck(substr($value, 1, strlen($value) - 2))) { $bin = true; } else { $bin = false; } } if ($bin) { $arValueList[] = "B'" . substr($value, 1, strlen($value) - 2) . "'"; } else { $arValueList[] = $this->conn->quoteSmart($value); } //} $count++; } $sFieldList = join(', ', array_keys($arValues)); $sValueList = implode(', ', $arValueList); //make sure the table name is properly escaped $tableName = $this->conn->quoteIdentifier($tableName); if ($prepare) { $questionMarks = array_pad(array(), count($arValues), '?'); $qmString = join(', ', $questionMarks); $sql = "INSERT INTO {$tableName} ( {$sFieldList}) VALUES ( {$qmString} )"; $resource = $this->prepare($sql); $result = $this->execute($resource, array_values($arValues)); } else { $sql = "INSERT INTO {$tableName} ( {$sFieldList}) VALUES ( {$sValueList} )"; $result = $this->conn->query($sql); } if (DB::isError($result)) { throw new LoggedException($result->getMessage(), $result->getCode(), self::module); } //return the ID, if there was one, or the number of rows affected return $id ? $id : $this->conn->affectedRows(); }