Example #1
0
function updateorinsert($table, $keyvalues, $values = array(), $insertonlyvalues = false)
{
    global $dbutils_show_errors;
    global $dbutils_history_callback;
    global $updateorinsert_inserted;
    global $dbutils_link;
    $i = false;
    $r = false;
    $allvalues = array_merge($keyvalues, $values);
    assertDataNonRO('updateorinsert:' . $table);
    $updateorinsert_inserted = false;
    txBegin();
    if (!isset($keyvalues['id']) || 0 + @$keyvalues['id'] != 0) {
        // if the key value is based on an ID that is nonzero, or the key value is based on something other than ID:
        $sql = 'SELECT * FROM `' . $table . '` WHERE ' . arraytosafe($keyvalues, true);
        if ($f = sqf($sql)) {
            $i = 0 + @$f['id'];
            $r = true;
            $sql = 'UPDATE `' . $table . '` SET ' . arraytosafe($allvalues) . ' WHERE id = ' . $i;
            mysql_query($sql, $dbutils_link);
            if ($dbutils_show_errors) {
                echo mysql_error($dbutils_link);
            }
        }
    }
    if (!$r) {
        if ($insertonlyvalues !== false) {
            $allvalues = array_merge($allvalues, $insertonlyvalues);
        }
        if (isset($allvalues['id'])) {
            // allow inserting to autogenerate the id field
            if ($allvalues['id'] == 0) {
                unset($allvalues['id']);
            }
        }
        $sql = 'INSERT INTO `' . $table . '` (`' . implode('`, `', array_keys($allvalues)) . '`) VALUES (';
        $first = true;
        foreach ($allvalues as $name => $val) {
            if (!$first) {
                $sql .= ', ';
            }
            if (gettype($val) == 'string') {
                $sql .= '"' . mes($val) . '"';
            } elseif (gettype($val) == 'array') {
                $sql .= $val[0];
                // raw expression
            } else {
                $sql .= 0 + @$val;
            }
            $first = false;
        }
        $sql .= ')';
        mysql_query($sql, $dbutils_link);
        $error = mysql_error($dbutils_link);
        if ($error > '' && $dbutils_show_errors) {
            echo $error . "\r\n";
        }
        $i = mysql_insert_id($dbutils_link);
        $updateorinsert_inserted = true;
    }
    if ($dbutils_history_callback !== false) {
        $dbutils_history_callback($table, $keyvalues);
    }
    txCommit();
    return $i;
}
Example #2
0
function updateorinsert($table = '', $keyvalues = array(), $values = array(), $insertonlyvalues = false, $stack_trace_level = 0)
{
    global $dbutils_show_errors;
    global $dbutils_history_callback;
    global $updateorinsert_inserted;
    global $dbutils_link;
    $i = false;
    $r = false;
    $allvalues = array_merge($keyvalues, $values);
    assertDataNonRO('updateorinsert:' . $table);
    if ('' . @$table == '') {
        dbiutils_stack_trace('No table specified', $stack_trace_level);
        return false;
    }
    if (!is_array($keyvalues)) {
        dbiutils_stack_trace('Match criteria must be specified as an array', $stack_trace_level);
        return false;
    }
    if (count($keyvalues) == 0) {
        dbiutils_stack_trace('No match criteria specified', $stack_trace_level);
        return false;
    }
    if (!dbiutils_assert_connection(0)) {
        return false;
    }
    $updateorinsert_inserted = false;
    txBegin();
    if (!isset($keyvalues['id']) || 0 + @$keyvalues['id'] != 0) {
        // if the key value is based on an ID that is nonzero, or the key value is based on something other than ID:
        $whereclause = arraytosafe($keyvalues, true);
        if ('' . @$whereclause == '') {
            dbiutils_stack_trace('Invalid match criteria', $stack_trace_level);
            txCancel();
            return false;
        }
        $sql = 'SELECT * FROM `' . $table . '` WHERE ' . $whereclause;
        if ($f = sqf($sql)) {
            $i = 0 + @$f['id'];
            $r = true;
            $valuestoset = arraytosafe($allvalues);
            if ($valuestoset == '') {
                dbiutils_stack_trace('No values specified', $stack_trace_level);
                txCancel();
                return false;
            }
            $sql = 'UPDATE `' . $table . '` SET ' . $valuestoset . ' WHERE id = ' . $i;
            mysqli_query($dbutils_link, $sql);
            $errortext = mysqli_error($dbutils_link);
            if ($errortext > '') {
                if ($dbutils_show_errors) {
                    dbiutils_stack_trace('MySQL: ' . $errortext . ' [QUERY: ' . $sql . ']', $stack_trace_level);
                }
                txCancel();
                return false;
            }
        }
    }
    if (!$r) {
        if ($insertonlyvalues !== false) {
            $allvalues = array_merge($allvalues, $insertonlyvalues);
        }
        if (isset($allvalues['id'])) {
            // allow inserting to autogenerate the id field
            if ($allvalues['id'] == 0) {
                unset($allvalues['id']);
            }
        }
        if (count($allvalues) == 0) {
            dbiutils_stack_trace('No values specified', $stack_trace_level);
            txCancel();
            return false;
        }
        $fieldnames = implode('`, `', array_keys($allvalues));
        if ('' . @$fieldnames == '') {
            dbiutils_stack_trace('Error compiling list of field names for insert', $stack_trace_level);
            txCancel();
            return false;
        }
        $sql = 'INSERT INTO `' . $table . '` (`' . $fieldnames . '`) VALUES (';
        $first = true;
        foreach ($allvalues as $name => $val) {
            if (!$first) {
                $sql .= ', ';
            }
            if (gettype($val) == 'string') {
                $sql .= '"' . mes('' . @$val) . '"';
            } elseif (gettype($val) == 'array') {
                if (!is_string($val[0])) {
                    dbiutils_stack_trace('When specified as an array (inline SQL fragment) value contained therein must be a string', $stack_trace_level);
                } else {
                    $sql .= '' . @$val[0];
                    // raw expression
                }
            } else {
                $sql .= 0 + @$val;
            }
            $first = false;
        }
        $sql .= ')';
        mysqli_query($dbutils_link, $sql);
        $error = mysqli_error($dbutils_link);
        if ($error > '') {
            if ($dbutils_show_errors) {
                dbiutils_stack_trace('MySQL: ' . $error . ' [QUERY: ' . $sql . ']', $stack_trace_level);
            }
            txCancel();
            return false;
        }
        $i = 0 + @mysqli_insert_id($dbutils_link);
        if ($i == 0) {
            dbiutils_stack_trace('No inserted record id was returned', $stack_trace_level);
            txCancel();
            return false;
        }
        $updateorinsert_inserted = true;
    }
    if ($dbutils_history_callback !== false) {
        @$dbutils_history_callback($table, $keyvalues);
    }
    if (txCommit()) {
        return $i;
    } else {
        return false;
    }
}