从<select multiple =“”>框</select>输入多个选择到数据库中

时间:2013-11-17 18:46:21

标签: php mysql sql database multiple-select

我有下面的代码,我想在“选择多个”框中选择多个选项,并将这些选项全部添加到数据库中。目前只输入了1个选定值。我是这样一个菜鸟,所以需要帮助来推进这个项目。

感谢您的帮助!

function ProjectTheme_get_categories($taxo, $selected = "", $include_empty_option = "",     $ccc = "")
{
$args = "orderby=name&order=ASC&hide_empty=0&parent=0";
$terms = get_terms( $taxo, $args );

$ret = '<select multiple size="20" name="'.$taxo.'_cat" class="'.$ccc.'" id="scrollselect     '.$ccc.'">';
if(!empty($include_empty_option)) $ret .= "<option value=''>".$include_empty_option."</o    ption>";

if(empty($selected)) $selected = -1;

foreach ( $terms as $term )
{
$id = $term->term_id;

$ret .= '<option '.($selected == $id ? "selected='selected'" : " " ).' value="'.$id.'">'.$term->name.'</option>';

$args = "orderby=name&order=ASC&hide_empty=0&parent=".$id;
$sub_terms = get_terms( $taxo, $args ); 

foreach ( $sub_terms as $sub_term )
{
    $sub_id = $sub_term->term_id; 
    $ret .= '<option '.($selected == $sub_id ? "selected='selected'" : " " ).' value="'.$sub_id.'">&nbsp; &nbsp;|&nbsp;  '.$sub_term->name.'</option>';

    $args2 = "orderby=name&order=ASC&hide_empty=0&parent=".$sub_id;
    $sub_terms2 = get_terms( $taxo, $args2 );   

    foreach ( $sub_terms2 as $sub_term2 )
    {
        $sub_id2 = $sub_term2->term_id; 
        $ret .= '<option '.($selected == $sub_id2 ? "selected='selected'" : " " ).' value="'.$sub_id2.'">&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; 
         '.$sub_term2->name.'</option>';

    }
}

}

$ret .= '</select>';

return $ret;

}

以下是用户与该表单交互的代码的另一部分

<li><h2><?php echo __('Category', 'ProjectTheme'); ?>:</h2>
        <p><?php    echo ProjectTheme_get_categories("project_cat",  
        !isset($_POST['project_cat_cat']) ? (is_array($cat) ? $cat[0]->term_id : "") : $_POST['project_cat_cat']
        , __("Select Category","ProjectTheme"), "do_input"); ?></p>
    </li>

处理表单提交功能的附加代码

do_action('ProjectTheme_post_new_post_post',$pid);

if(isset($_POST['project_submit1']))
{


    $project_title          = trim($_POST['project_title']);
    $project_description    = nl2br($_POST['project_description']);
    $project_category       = $_POST['project_cat_cat'];
    $project_location       = trim($_POST['project_location_cat']);
    $project_tags           = trim($_POST['project_tags']);
    $price                  = trim($_POST['price']);
    $project_location_addr  = trim($_POST['project_location_addr']);
    $end                    = trim($_POST['ending']); 

    update_post_meta($pid, 'finalised_posted', '0');

    //--------------------------------

    $projectOK = 1;

    if(empty($project_title))       { $projectOK = 0; $MYerror['title']         = __('You cannot leave the project title blank!','ProjectTheme'); }
    if(empty($project_category)) { $projectOK = 0; $MYerror['cate']     = __('You cannot leave the project category blank!','ProjectTheme'); }
    if(empty($project_description)) { $projectOK = 0; $MYerror['description']   = __('You cannot leave the project description blank!','ProjectTheme'); }

    //--------------------------------


    $project_category2  = $project_category;

    $my_post = array();

    $my_post['post_title']      = $project_title;
    $my_post['post_status']     = 'draft';
    $my_post['ID']              = $pid;
    $my_post['post_content']    = $project_description;

    $term = get_term( $project_category, 'project_cat' );   
    $project_category = $term->slug;

    $term = get_term( $project_location, 'project_location' );  
    $project_location = $term->slug;

    wp_update_post( $my_post );
    wp_set_object_terms($pid, array($project_category),'project_cat');
    wp_set_object_terms($pid, array($project_location),'project_location');

    wp_set_post_tags( $pid, $project_tags);

2 个答案:

答案 0 :(得分:0)

[]附加到选择标记的名称,如<select name="taxo_cat[]" multiple>,在您的PHP代码中,您可以通过调用$_POST['taxo_cat']

来访问值数组

答案 1 :(得分:0)

首先,为了能够保存所有选定的选项,您必须将它们作为数组发送。为此,元素的名称必须是

name="'.$taxo.'_cat[]"

之后,在服务器端,您将在请求变量中找到所有选定的值。

我不熟悉Wordpress,但代码中发生的变化是

$project_category = $_POST['project_cat_cat']; 
$project_location = trim($_POST['project_location_cat']);` 

project_category和project_location现在是数组而不是字符串,这意味着你必须改变调用函数get_term()的方式,并且

wp_set_object_terms($pid, array($project_category),'project_cat');
wp_set_object_terms($pid, array($project_location),'project_location');

必须更改为

wp_set_object_terms($pid, $project_category,'project_cat');
wp_set_object_terms($pid, $project_location,'project_location');