$value) { if(!is_numeric($key)) $key = ticks($key); $sql .= "when $key then ". sql_escape_string($value) . " "; } $sql .= "else '' end"; return $sql; } /** * Take a Associated Array and Table name and insert the array's values into the database. * Array should be in the format $arrayname['fieldname'] => value * * This function will escape Everything so please don't escape before hand. * * @param array $inArray the array to be inserted * @param string $tablename the name of the table to insert the array into * @return mixed $return the primary key value of the inserted record */ function db_insert_array(&$inArray, &$tablename) { sql_connect(); global $defaultdb; foreach ($inArray as $field => $value) { $fields[] = $defaultdb->escape_identifier($field); $values[] = $defaultdb->escape_string($value); } $fields = implode(",", $fields); $values = implode(",", $values); $tablename = $defaultdb->escape_tablename($tablename); $query = "INSERT INTO $tablename ($fields) VALUES ($values)"; $id = sql_insert($query); return $id; } /** * Take a Associated Array and Table name, and Primary Key name and Id and update the database with the values in the array. * Array should be in the format $arrayname['fieldname'] => value * * This function will escape Everything so please don't escape before hand. * * @param array $inArray the array to be inserted * @param string $tablename the name of the table to insert the array into * @param string $primarykey the name of the primary key field * @param string $id of the primary key */ function db_update_array($inArray, $tablename, $primarykey, $primarykeyvalue) { sql_connect(); global $defaultdb; foreach ($inArray as $field => $value) { $updateStr = ""; $updateStr .= $defaultdb->escape_identifier($field); $updateStr .= "="; $updateStr .= $defaultdb->escape_string($value); $updateArray[] = $updateStr; } $newupdateStr = implode(",", $updateArray); $tablename = $defaultdb->escape_tablename($tablename); $primarykey = $defaultdb->escape_identifier($primarykey); $primarykeyvalue = $defaultdb->escape_string($primarykeyvalue); $query = "UPDATE $tablename SET $newupdateStr WHERE $primarykey = $primarykeyvalue"; return $defaultdb->query($query); } /** * A Wrapper Function for update_array and insert_array. Examines the $primarykeyvalue * and chooses which function to run. * * This function will escape Everything so please don't escape before hand. * * @param array $inArray the array to be inserted * @param string $tablename the name of the table to insert the array into * @param string $primarykey the name of the primary key field * @param string $id of the primary key * @return mixed $return the primary key value of the saved record */ function db_save_array($inArray, $tablename, $primarykey, $primarykeyvalue) { if ($primarykeyvalue == "new") { $return = db_insert_array($inArray, $tablename); } else { db_update_array($inArray, $tablename, $primarykey, $primarykeyvalue); $return = $primarykeyvalue; } return $return; }