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

Inserts a row into the table.

Description

Examples:

$wpdb->insert(
    'table',
    array(
        'column1' => 'foo',
        'column2' => 'bar',
    )
);
$wpdb->insert(
    'table',
    array(
        'column1' => 'foo',
        'column2' => 1337,
    ),
    array(
        '%s',
        '%d',
    )
);

See also

Parameters

$tablestringrequired
Table name.
$dataarrayrequired
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.
$formatstring[]|stringoptional
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

Return

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

Source

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

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

  1. Skip to note 7 content

    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 );
  2. Skip to note 8 content
    global $wpdb;
    
    //get my table
    $tableUsers = $wpdb->prefix . 'wpdocs_users';
    
    $name = 'mostafa';
    $family = 'niakan';
    $phone = '+9812345678';
    
    $data = array( 
        'name'   => $name,
        'family' => $family,
        'phone'  => $phone,
    );
    $format = array( '%s', '%s', '%s', '%d' );
    $stmt = $wpdb->insert( $tableUsers, $data, $format );
    
    //get insert id
    var_dump( $wpdb->insert_id );

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