嗨,我当前正在编写一个WordPress插件,该插件需要将6行更新到自定义WordPress数据库。我正在尝试使用一个foreach循环来遍历通过AJAX表单提交的数据。然后,我想遍历此数据并将每一行更新到自定义数据库。这是目前的代码。该解决方案目前无法正常工作,我不确定如何最好地解决此问题?我猜有一个更简单的解决方案?
public function ajax_form_action() {
if (isset($_POST)) {
// unserialize the data
parse_str($_POST["data"], $_POST);
if (!$errNotice) {
global $wpdb; // this is how you get access to the database
$table_name = $wpdb->prefix . "pl_actuals";
foreach ($_POST['ly_id'] as $id){
//$id = $_POST['ly_id'];
$unit = $_POST['venue'];
$week = $_POST['ly_week'];
$week_ending_date = $_POST['ly_week_ending_date'];
$sun_net_sales = $_POST['ly_sun_net_sales'];
$mon_net_sales = $_POST['ly_mon_net_sales'];
$tues_net_sales= $_POST['ly_tues_net_sales'];
$wed_net_sales= $_POST['ly_wed_net_sales'];
$thurs_net_sales= $_POST['ly_thurs_net_sales'];
$fri_net_sales= $_POST['ly_fri_net_sales'];
$sat_net_sales= $_POST['ly_sat_net_sales'];
$total_net_sales= $_POST['ly_total_net_sales'];
$total_budget_net_sales= $_POST['ly_budget_net_sales'];
$total_labour= $_POST['ly_total_labour'];
$count = count ($id);
for ($i=0; $i < $count; $i++ ) {
$rows_affected = $wpdb->update( $table_name, array(
'sun_net_sales' => $sun_net_sales [$i],
'mon_net_sales' => $mon_net_sales [$i],
'tues_net_sales' => $tues_net_sales [$i],
'wed_net_sales' => $wed_net_sales [$i],
'thurs_net_sales' => $thurs_net_sales [$i],
'fri_net_sales' => $fri_net_sales [$i],
'sat_net_sales' => $sat_net_sales [$i],
'total_net_sales' => $total_net_sales [$i],
'total_budget_net_sales' => $total_budget_net_sales [$i],
'total_labour' => $total_labour [$i]
),array(
'id' => $id));
if($rows_affected==1){
echo "Your Forecast has been submitted'. $id .'";
} else {
exit( var_dump( $wpdb->last_error ));
}
++$i;
$wpdb->flush();
}
}
} else {
echo $errNotice;
// stop executing script
die();
}
} // end if