I have several fruit checkboxes but when saving to the wpdb database it only saves the last one that it checks, variable $check receives vardump string (6) "banana" string (5) "apple"
In the database only appears apple should be banana, apple in that same field an array. Should not only save it the last marked
foreach( $checkboxes as $check ) { var_dump( $check); } global $wpdb; $wpdb->insert('data',array( 'fruit' => $check ));
I have several fruit checkboxes but when saving to the wpdb database it only saves the last one that it checks, variable $check receives vardump string (6) "banana" string (5) "apple"
In the database only appears apple should be banana, apple in that same field an array. Should not only save it the last marked
foreach( $checkboxes as $check ) { var_dump( $check); } global $wpdb; $wpdb->insert('data',array( 'fruit' => $check ));Share Improve this question edited Jul 6, 2020 at 8:55 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jul 6, 2020 at 4:42 StymarkStymark 372 bronze badges 1 |
1 Answer
Reset to default 1This isn't a Wordpress question this is a PHP question. Your foreach
loops through all the $checkboxes
putting one in the $check
variable each time, so your insert()
call only inserts the last value for $check
, because it's not inside that foreach
loop.
You probably want:
global $wpdb;
foreach( $checkboxes as $check ) {
$wpdb->insert('data',array('fruit' => $check));
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742289322a4415897.html
$checkboxes
, not$check
, which can only be one item because it's created in theforeach
loop. – Jacob Peattie Commented Jul 6, 2020 at 6:19