How to Update multiple rows using $wpdb->update

admin2025-01-07  4

I have created dynamic add/remove fields in a frontend post submission page. Using $wpdb->insert, the system is working perfectly.

I have also created a draft edit page on the frontend. On this page I want these dynamic fields to get updated. I tried $wpdb->update but it only updates the last field. In short I want to update multiple rows on single click.

my code:

$project_id = $_SESSION['project_id'];
global $wpdb;
$reward_ids = $wpdb->get_results("SELECT * FROM wpxa_rewards WHERE project_id = $project_id");
foreach($reward_ids as $reward_id); $r_id = $reward_id->ID;

$count = count( $project_reward_title );
for ( $i = 0; $i < $count; $i++ ) {

global $wpdb;

$wpdb->update( 'wpxa_rewards',

  array(

    'reward_title'       => "$project_reward_title[$i]",
    'reward_description' => "$project_reward_description[$i]",
    'reward_amount'      => "$project_reward_amount[$i]",
    'reward_shipping'    => "$project_reward_shipping[$i]",
    'est_date'           => "$project_est_date[$i]"

  ),

  array( 'ID' => $r_id ),

  array(

    '%s',
    '%s',
    '%d',
    '%s',
    '%s'

  ),

  array( '%d' )

);

}

Plz help.. Thanks

I have created dynamic add/remove fields in a frontend post submission page. Using $wpdb->insert, the system is working perfectly.

I have also created a draft edit page on the frontend. On this page I want these dynamic fields to get updated. I tried $wpdb->update but it only updates the last field. In short I want to update multiple rows on single click.

my code:

$project_id = $_SESSION['project_id'];
global $wpdb;
$reward_ids = $wpdb->get_results("SELECT * FROM wpxa_rewards WHERE project_id = $project_id");
foreach($reward_ids as $reward_id); $r_id = $reward_id->ID;

$count = count( $project_reward_title );
for ( $i = 0; $i < $count; $i++ ) {

global $wpdb;

$wpdb->update( 'wpxa_rewards',

  array(

    'reward_title'       => "$project_reward_title[$i]",
    'reward_description' => "$project_reward_description[$i]",
    'reward_amount'      => "$project_reward_amount[$i]",
    'reward_shipping'    => "$project_reward_shipping[$i]",
    'est_date'           => "$project_est_date[$i]"

  ),

  array( 'ID' => $r_id ),

  array(

    '%s',
    '%s',
    '%d',
    '%s',
    '%s'

  ),

  array( '%d' )

);

}

Plz help.. Thanks

Share Improve this question edited Jan 29, 2018 at 11:42 janh 2,74812 silver badges13 bronze badges asked Jan 29, 2018 at 7:34 MineshMinesh 3173 silver badges15 bronze badges 2
  • Don't use "$project_reward_title[$i]", just write $project_reward_title[$i], it'll only confuse the interpreter. – janh Commented Jan 29, 2018 at 8:40
  • @janh Thanks. But it doesn't work... – Minesh Commented Jan 29, 2018 at 8:50
Add a comment  | 

1 Answer 1

Reset to default 1

It is a a bit of a stab in the dark, but I'm fairly confident that the problem is your foreach loop. It's broken (it will not execute anything because only an empty statement (;) is affected, and you will probably want it to have a block that contains the rest of the code.

As an example:

<?php
foreach(array(1, 2, 3) as $test);  
print $test . "\n";

will only print 3, while

<?php
foreach(array(1, 2, 3) as $test) // note the lack of the semicolon.
print $test . "\n";

will print a line for 1, 2 and 3. As a general rule, I recommend always being explicit with what you want to be repeated in a loop. Use curly braces, e.g.

<?php
foreach(array(1, 2, 3) as $test) {
    print $test . "\n";
}

It's easy to read and keeps you safe from these kind of bugs. If you have to add another line in the loop, just add it before the closing curly braces.

This code will probably be what you want.

$project_id = $_SESSION['project_id'];
global $wpdb;
$reward_ids = $wpdb->get_results("SELECT * FROM wpxa_rewards WHERE project_id = $project_id");
foreach($reward_ids as $reward_id) {
    $r_id = $reward_id->ID;

    $count = count( $project_reward_title );
    for ( $i = 0; $i < $count; $i++ ) {

        $wpdb->update( 'wpxa_rewards',

        array(

            'reward_title'       => $project_reward_title[$i],
            'reward_description' => $project_reward_description[$i],
            'reward_amount'      => $project_reward_amount[$i],
            'reward_shipping'    => $project_reward_shipping[$i],
            'est_date'           => $project_est_date[$i]

        ),

        array( 'ID' => $r_id ),

        array(

            '%s',
            '%s',
            '%d',
            '%s',
            '%s'

        ),

        array( '%d' )

        );

    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261973a782.html

最新回复(0)