admin管理员组文章数量:1332896
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));
}
本文标签: Save data from a checkbox to a wpdb array
版权声明:本文标题:Save data from a checkbox to a wpdb array 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289321a2447504.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