duancao2082 2015-10-30 17:07
浏览 59

将多选值作为数组发布

I've been putting together an advanced post notification system for wordpress custom posts, based on Carlo Daniele's post here: http://www.smashingmagazine.com/2015/05/building-wordpress-notification-system/

I'm trying to modify it so that user can select multiple States/Areas, where I'm running into trouble is posting the selected states to the database.

    $smashing_notification_states = array( '- Dublin City Centre -' => '- Dublin City Centre -', '- North Dublin City -' => '- North Dublin City -', 
'- South Dublin City -' => '- South Dublin City -', 
'- North Co. Dublin -' => '- North Co. Dublin -', 
'- South Co. Dublin -' => '- South Co. Dublin -', 
'- West Co. Dublin -' => '- West Co. Dublin -', );

Select Area: `

        <td>
            <label for="state">
                <select  multiple="multiple"  name="state">
                    <option value="" <?php selected( get_user_meta( $user->ID, 'state', true ), "" ); ?>>Select</option>
                    <?php foreach ($smashing_notification_states as $key => $value) { ?>
                        <option value="<?php echo $key; ?>" <?php selected( esc_attr( get_user_meta( $user->ID, 'state', true ) ), $key ); ?>><?php echo $value; ?></option>
                    <?php } ?>
                </select>
                <?php _e( 'Select area', 'smashing' ); ?>
            </label>
        </td>
    </tr>`

if( isset($_POST['state']) ) update_user_meta( $user_id, 'state', sanitize_text_field( $_POST['state'] ) );

I've pushed and pulled at it but to no avail, tried. Last thing I tried was changing <select multiple="multiple" name="state"> to <select multiple="multiple" name="state[]">

This posted as "Array"

Any help would be great as always!

  • 写回答

1条回答 默认 最新

  • douan4359 2015-10-30 18:01
    关注

    Using <select multiple="multiple" name="state[]"> works because you are getting "Array" as a value. The problem is that a database doesn't understand PHP data types, so you need to convert it into something it will understand... a quick search:

    Insert array into MySQL database with PHP

    Or you can opt to serialize the data and insert it that way as well:

    http://php.net/manual/fr/function.serialize.php

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能