public function quantum_insert(array $data = array())
{
$quantum_fields = array();
$infinity_ref = array();
// bug fix: to get only the relevant fields on specific page.
$field_list = flatten_array($this->fields);
// to generate $infinity_ref and $quantum_fields as reference and validate the $_POST input value.
foreach ($field_list as $field_id => $field_data) {
if ($field_data['field_parent'] == $this->input_page) {
$target_database = $field_data['field_cat_db'] ? DB_PREFIX . $field_data['field_cat_db'] : DB_USERS;
$target_index = $field_data['field_cat_index'] ? $field_data['field_cat_index'] : 'user_id';
$index_value = isset($_POST[$target_index]) ? form_sanitizer($_POST[$target_index], 0) : $data[$target_index];
// create reference array
$infinity_ref[$target_database] = array('index' => $target_index, 'value' => $index_value);
if (isset($_POST[$field_data['field_name']])) {
$quantum_fields[$target_database][$field_data['field_name']] = form_sanitizer($_POST[$field_data['field_name']], $field_data['field_default'], $field_data['field_name']);
} else {
$quantum_fields[$target_database][$field_data['field_name']] = isset($data['field_name']) ? $data[$field_data['field_name']] : '';
}
}
}
if (!empty($quantum_fields)) {
$temp_table = '';
foreach ($quantum_fields as $_dbname => $_field_values) {
$merged_data = array();
$merged_data += $_field_values;
$merged_data += $data;
// appends all other necessary values to fill up the entire table values.
if ($temp_table !== $_dbname) {
// if $temp_table is different. check if table exist. run once if pass
$merged_data += array($infinity_ref[$_dbname]['index'] => $infinity_ref[$_dbname]['value']);
// Primary Key and Value.
// ensure nothing is missing. this might be overkill. I would shut it down if not neccessary to lighten the load by 1-2 uncessary query.
$result = dbquery("SELECT * FROM " . $_dbname . " WHERE " . $infinity_ref[$_dbname]['index'] . " = '" . $infinity_ref[$_dbname]['value'] . "'");
if (dbrows($result) > 0) {
$merged_data += dbarray($result);
}
}
dbquery_insert($_dbname, $merged_data, 'update');
}
}
}