Wordpress $wpdb. Insert Multiple Records Wordpress $wpdb. Insert Multiple Records wordpress wordpress

Wordpress $wpdb. Insert Multiple Records


OK, I figured it out!

Setup arrays for Actual Values, and Placeholders

$values = array();$place_holders = array();

the initial Query:

$query = "INSERT INTO orders (order_id, product_id, quantity) VALUES ";

Then loop through the the values you're looking to add, and insert them in the appropriate arrays:

foreach ( $_POST as $key => $value ) {     array_push( $values, $value, $order_id );     $place_holders[] = "('%d', '%d')" /* In my case, i know they will always be integers */}

Then add these bits to the initial query:

$query .= implode( ', ', $place_holders );$wpdb->query( $wpdb->prepare( "$query ", $values ) );


You can also use this way to build the query:

$values = array();// We're preparing each DB item on it's own. Makes the code cleaner.foreach ( $items as $key => $value ) {    $values[] = $wpdb->prepare( "(%d,%d)", $key, $value );}$query = "INSERT INTO orders (order_id, product_id, quantity) VALUES ";$query .= implode( ",\n", $values );


I have came across with this problem and decided to build a more improved function by using accepted answer too:

/** * A method for inserting multiple rows into the specified table *  *  Usage Example:  * *  $insert_arrays = array(); *  foreach($assets as $asset) { * *  $insert_arrays[] = array( *  'type' => "multiple_row_insert", *  'status' => 1, *  'name'=>$asset, *  'added_date' => current_time( 'mysql' ), *  'last_update' => current_time( 'mysql' )); * *  } * *  wp_insert_rows($insert_arrays); * * * @param array $row_arrays * @param string $wp_table_name * @return false|int * * @author  Ugur Mirza ZEYREK * @source http://stackoverflow.com/a/12374838/1194797 */function wp_insert_rows($row_arrays = array(), $wp_table_name) {    global $wpdb;    $wp_table_name = esc_sql($wp_table_name);    // Setup arrays for Actual Values, and Placeholders    $values = array();    $place_holders = array();    $query = "";    $query_columns = "";    $query .= "INSERT INTO {$wp_table_name} (";            foreach($row_arrays as $count => $row_array)            {                foreach($row_array as $key => $value) {                    if($count == 0) {                        if($query_columns) {                        $query_columns .= ",".$key."";                        } else {                        $query_columns .= "".$key."";                        }                    }                    $values[] =  $value;                    if(is_numeric($value)) {                        if(isset($place_holders[$count])) {                        $place_holders[$count] .= ", '%d'";                        } else {                        $place_holders[$count] .= "( '%d'";                        }                    } else {                        if(isset($place_holders[$count])) {                        $place_holders[$count] .= ", '%s'";                        } else {                        $place_holders[$count] .= "( '%s'";                        }                    }                }                        // mind closing the GAP                        $place_holders[$count] .= ")";            }    $query .= " $query_columns ) VALUES ";    $query .= implode(', ', $place_holders);    if($wpdb->query($wpdb->prepare($query, $values))){        return true;    } else {        return false;    }}

Source: https://github.com/mirzazeyrek/wp-multiple-insert