wpdb::insert( string $table, array $data, array|string $format = null ): int|false

Inserts a row into the table.


Description

Examples:

wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )

Top ↑

See also


Top ↑

Parameters

$table string Required
Table name.
$data array Required
Data to insert (in column => value pairs).
Both $data columns and $data values should be "raw" (neither should be SQL escaped).
Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
$format array|string Optional
An array of formats to be mapped to each of the value in $data.
If string, that format will be used for all of the values in $data.
A format is one of '%d', '%f', '%s' (integer, float, string).
If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.

Default: null


Top ↑

Return

int|false The number of rows inserted, or false on error.


Top ↑

Source

File: wp-includes/wp-db.php. View all references

public function insert( $table, $data, $format = null ) {
	return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
}


Top ↑

Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 2 content
    Contributed by shewa12

    Insert multiple rows at once

    function insert_multiple_rows( $table, $request ) {
        global $wpdb;
        $column_keys   = '';
        $column_values = '';
        $sql           = '';
        $last_key      = array_key_last( $request );
        $first_key     = array_key_first( $request );
        foreach ( $request as $k => $value ) {
            $keys = array_keys( $value );
    
            // Prepare column keys & values.
            foreach ( $keys as $v ) {
                $column_keys   .= sanitize_key( $v ) . ',';
                $sanitize_value = sanitize_text_field( $value[ $v ] );
                $column_values .= is_numeric( $sanitize_value ) ? $sanitize_value . ',' : "'$sanitize_value'" . ',';
            }
            // Trim trailing comma.
            $column_keys   = rtrim( $column_keys, ',' );
            $column_values = rtrim( $column_values, ',' );
            if ( $first_key === $k ) {
                $sql .= "INSERT INTO {$table} ($column_keys) VALUES ($column_values),";
            } elseif ( $last_key == $k ) {
                $sql .= "($column_values)";
            } else {
                $sql .= "($column_values),";
            }
    
            // Reset keys & values to avoid duplication.
            $column_keys   = '';
            $column_values = '';
        }
        return $wpdb->query( $sql );
    }
    
    // Usage
    $table = 'table_name';
    $data = array(
        array( 'id' => 1, 'name' => 'John' ),
        array( 'id' => 2, 'name' => 'Doe' ),
    );
    
    insert_multiple_rows( $table, $data );

You must log in before being able to contribute a note or feedback.