开发者

Wordpress insert multiple rows into a data table

开发者 https://www.devze.com 2023-03-30 09:35 出处:网络
i want to insert some data into a data table, for a wordpress plugin. the data is taken with POST. i have multiple results (taken with post), but $wpdb->insert only inserts me the last result (practic

i want to insert some data into a data table, for a wordpress plugin. the data is taken with POST. i have multiple results (taken with post), but $wpdb->insert only inserts me the last result (practically overwrites the data). why is that?

here is my code:

html:

echo '<label for="reduceri-post-category"><b>' . __("What categories should be the adverts", 'appplugin' ) . '</b></label><br /><br /><br />';
    ?>

    <?php foreach ($the_data[categories]  as $sat): ?>
    <b> <?= $sat[name]; ?> <br /> </b>
            <?php foreach ($sat[subcategories] as  $cat):
    ?>

    &nbsp; &nbsp; &开发者_Go百科amp;nbsp;<input type="checkbox" name="reduceri-post-category" value="<?= $cat[sid] ?>" /> <?php echo $cat[name]; echo $cat[sid]; ?><br />

    <? endforeach; ?>
    <? endforeach; ?>




global $wpdb;


    $thedata['reduceri-post-category'] = $_POST['reduceri-post-category'];


    $table_name = $wpdb->prefix . "reduceri";


    foreach ($thedata as $key => $value) { 
    if( $post->post_type == 'revision' ) return;
    if ( $post->post_type == 'post'){

        $wpdb->insert($table_name, array( 'time' => current_time('mysql'), 'post' => $post->ID, 'category' => $value));
    }
    }

what can i do in order to be able to insert ALL the results, not only the last one?? thanks a lot!


What is in reduceri-post-category?

You say that you have multiple values in post. How are these multiple values passed into your plugin? Does reduceri-post-category contain multiple values? Do you use separate keys for each of the values? For example, reduceri-post-category2/3/4?

You are using a foreach to iterate through $thedata. However I don't see anywhere in your code where you actually create an array in $thedata. So your foreach is only ever going to execute once, and it will execute based on what is inside $_POST['reduceri-post-category'];

What I think you want to do, tough to say, is one of these two scenarios.

Scenario 1 - multiple post keys hold data you are after

$thedata[foo1] = $_POST[foo1];
$thedata[foo2] = $_POST[foo2];
$thedata[foo3] = $_POST[foo3];
foreach ($thedata as $key => $value) { }

Or (pseudeocode) - a single post key holds all your category data. So you have to split it up and then execute on each one.

$thedata = explode("?", $_POST[reduceri-post-category]);
foreach ($thedata as $key => $value) { }    
0

精彩评论

暂无评论...
验证码 换一张
取 消