$wpdb->insert – inserting multiple rows

I guess the problem is a pretty simple one. You didn’t show the whole code, but I guess the problem is that you’re saving the insertion for some unknown reason as string into a var named $stat. Everytime you fill the var with a new query you’re overwriting your var. You should use $stat .= $wpdb->insert( ...etc... ) instead. to append to your string. Don’t forget to set the var to an empty string before the loop, so you can append: $stat="".


Another – imo better way – would be to use an array. Reason: You can easier debug the code. And pls read the comment i/t code.

$stats = array();
for( $i = 0; $i <= $urlCount; $i++ ) 
{
    $stats[] = array(
        'POSTID' => $post->ID,
        // are you really retrieving **ALL** your urls as "url1", "url2", etc. 
        // via some $_POST-ed form?
        'URL' =>   filter_var(
            $_POST["url{$i}"], 
            FILTER_VALIDATE_URL, 
            FILTER_FLAG_SCHEME_REQUIRED
        )
    );
}

foreach ( $stats as $stat )
    $wpdb->insert( 'WP_URLS', $stat );

Leave a Comment