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
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' )
);
}
}
"$project_reward_title[$i]"
, just write$project_reward_title[$i]
, it'll only confuse the interpreter. – janh Commented Jan 29, 2018 at 8:40