Replace a row in a table if it exists or insert a new row if not exist.

@param string $table      → table name
@param array  $data       → column name and value
@param array  $statements → array with prepared parameters

@return object → query response
763931
public function replace($table, $data, $statements)
    {
        $columns = array_keys($data);
        $columnIdName = $columns[0];

        if (isset($statements[0][1]) && count($data) == count($statements)) {
            $id = $statements[0][1];
        } else {
            $id = array_shift($data);
        }

        $where = $columnIdName . ' = ' . $id;

        $result = $this->select(
            $columns,
            $table,
            $where,
            null,
            1,
            $statements
        );

        if ($this->rowCount($result)) {
            return $this->update($table, $data, $statements, $where);
        }

        return $this->insert($table, $data, $statements);
    }
1254027
public function replace($table, array $columns = null, array $where = null): ?array
    {
        if (\is_array($table)) {
            $columns = $this->getColumns($table, false);
            $table = $this->getTable($table);
            $where = $table;
        } elseif ($where) {
            $where = [SQL::WHERE => $where];
        }

        if (!\is_array($columns) || empty($columns)) {
            return null;
        }

        $map = [];
        $stack = [];

        foreach ($columns as $column => $replacements) {
            if (\is_array($replacements)) {
                foreach ($replacements as $old => $new) {
                    $mapKey = $this->mapKey();

                    $stack[] = $this->columnQuote($column) . ' = REPLACE(' . $this->columnQuote($column) . ', ' . $mapKey . 'a, ' . $mapKey . 'b)';

                    $map[$mapKey . 'a'] = [$old, PDO::PARAM_STR];
                    $map[$mapKey . 'b'] = [$new, PDO::PARAM_STR];
                }
            }
        }

        if ($stack === []) {
            return null;
        }

        $sql = 'UPDATE ' . $this->tableQuote($table) . ' SET ' .
            \implode(', ', $stack) . $this->suffixClause($where, $map);

        return [$sql, $map];
    }
528559
public function update($table, array $data)
    {
        $this->queryBuilder->update($table, $this->asData($data));
        return $this;
    }
304755
public function update($statements, array $data)
    {
        if (!isset($statements['tables'])) {
            throw new Exception('No table specified', 3);
        } elseif (count($data) < 1) {
            throw new Exception('No data given.', 4);
        }

        $table = end($statements['tables']);

        // Update statement
        list($updateStatement, $bindings) = $this->getUpdateStatement($data);

        // Wheres
        list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE');

        // Limit
        $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : '';

        $sqlArray = array(
            'UPDATE',
            $this->wrapSanitizer($table),
            'SET ' . $updateStatement,
            $whereCriteria,
            $limit
        );

        $sql = $this->concatenateQuery($sqlArray);

        $bindings = array_merge($bindings, $whereBindings);
        return compact('sql', 'bindings');
    }
575050
public function Insert($table, $data = array())
    {
        if (empty($table) || empty($data)) {
            return false;
        }

        //Before we set the columns, make sure we only use the column that re meant to be filled.
        foreach ($data as $key => $val) {
            if (empty($val)) {
                unset($data[$key]);
            }
        }

        //Get all the columns that are being set
        $columns = array_keys($data);

        //If the columns are not empty, proceed with the save
        if (!empty($columns)) {
            //Create a newInsert object
            $insert = $this->queryFactory->newInsert();

            //Set the table
            $insert->into($table);

            //Set the columns
            $insert->cols($columns);

            //Bind the values to be saved
            $insert->bindValues($data);

            //return the insert id
            return $this->exec($insert->getStatement(), $insert->getBindValues());

        }

        return false;
    }
381996
function insert( $rows, $method = null ) {

        return $this->db->insert( $this->table, $rows, $method );

    }
206304
public function insert($table, $row)
	{
		$keys = implode('`, `', array_keys($row));
        $holds = implode(',', array_fill(0, count($row), '?'));
		$vals = array_values($row);

		$sql = "INSERT INTO `{$table}` (`{$keys}`) VALUES({$holds})";

        $stmt = $this->prepare($sql);
        return $stmt ? $stmt->execute($vals) : false;
	}
879387
function insert($table, $data, $stmt_key = null)
    {
        return $this->executeBySyntax(self::INSERT, $table, $data, null, array(), $stmt_key);
    }
940216
protected function insertDefault( $table, $rows ) {

        $columns = $this->getColumns( $rows );
        if ( empty( $columns ) ) return;

        $query = $this->insertHead( $table, $columns );
        $lists = $this->valueLists( $rows, $columns );

        foreach ( $lists as $list ) {

            $singleQuery = $query . $list;

            $this->onQuery( $singleQuery );

            $statement = $this->prepare( $singleQuery );
            $statement->execute();

        }

        return $statement; // last statement is returned

    }
353911
private function doInsert(array $statements, array $data, $type): array
    {
        $table = end($statements['tables']);

        $bindings = $keys = $values = [];

        foreach ($data as $key => $value) {
            $keys[] = $key;
            if ($value instanceof Raw) {
                $values[] = (string)$value;
            } else {
                $values[] = '?';
                $bindings[] = $value;
            }
        }

        $sqlArray = [
            $type . ' INTO',
            $this->wrapSanitizer($table),
            '(' . $this->arrayStr($keys) . ')',
            'VALUES',
            '(' . $this->arrayStr($values, ',', false) . ')',
        ];

        if (isset($statements['onduplicate']) === true) {

            if (\count($statements['onduplicate']) < 1) {
                throw new Exception('No data given.', 4);
            }

            [$updateStatement, $updateBindings] = $this->getUpdateStatement($statements['onduplicate']);
            $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement;
            $bindings = array_merge($bindings, $updateBindings);

        }

        $sql = $this->concatenateQuery($sqlArray);

        return compact('sql', 'bindings');
    }
105713
public function insert($table, array $fields)
    {
        $query = new Insert($this, $table, $fields);
        return $query->execute();
    }
681803
public function update($table, $data, $statements, $where)
    {
        $query = 'UPDATE `' . $table . '`  SET ';

        foreach ($data as $column => $value) {
            $value = (is_null($statements) && is_string($value)) ? "'$value'" : $value;
            $query .= $column . '=' . $value . ', ';
        }

        $query = rtrim(trim($query), ',');

        $query .= (! is_null($where)) ? ' WHERE ' : '';

        $query .= (is_string($where)) ? $where . ' ' : '';

        if (is_array($where)) {
            foreach ($where as $clause) {
                $query .= $clause . ' AND ';
            }
            $query = rtrim(trim($query), 'AND');
        }

        if (! is_null($statements) && is_array($statements)) {
            return $this->statements($query, $statements);
        }

        return $this->query($query, 'INSERT');
    }
873476
public function insert(array $data, $table = null)
    {
        $table = ($table) ?: $this->getTable();
        $sql = $this->getSql();
        $insert = $sql->insert($table);

        $insert->values($data);

        $statement = $sql->prepareStatementForSqlObject($insert);
        $result = $statement->execute();

        return $result->getGeneratedValue();
    }
519178
public function insert($table, array $data, $prepare = true)
    {
        if ($prepare) {
            $data = $this->prepareInsert($table, $data);
        }

        if (empty($data)) {
            return '0';
        }

        $keys = array_keys($data);
        $fields = implode(',', $keys);
        $values = ':' . implode(',:', $keys);

        $this->run("INSERT INTO $table ($fields) VALUES ($values)", $data);
        return $this->pdo->lastInsertId();
    }
821579
public function insertId($table, $data)
    {
        try {
            $this->insert($table, $data);
            return $this->sql->lastInsertId();
        } catch (\Exception $e) {
            Throw $e;
        }
    }
784723
function insert($table, array $bind)
    {
        $sql = $this->insertSql($table, $bind);
        // execute the statement and return the number of affected rows
        $stmt = $this->wrappedAdapter->query($sql, array_values($bind));
        $result = $stmt->rowCount();
        return $result;
    }
467248
public function update(array $data, $where, $table = null)
    {
        $table = ($table) ?: $this->getTable();
        $sql = $this->getSql();
        $update = $sql->update($table);

        $update->set($data)
            ->where($where);

        $statement = $sql->prepareStatementForSqlObject($update);
        $result = $statement->execute();

        return $result->getAffectedRows();
    }