【问题标题】:Entering multiple selections into database from <select multiple> box从 <select multiple> 框中将多个选择输入数据库
【发布时间】:2013-11-17 18:46:20
【问题描述】:

我有下面的代码,我想在“选择多个”框中选择多个选项,并将这些都添加到数据库中。目前只输入选择的值中的 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);

【问题讨论】:

    标签: php mysql sql database multiple-select


    【解决方案1】:

    [] 附加到像&lt;select name="taxo_cat[]" multiple&gt; 这样的选择标签名称中,在您的php 代码中,您可以通过调用$_POST['taxo_cat'] 来访问值数组

    【讨论】:

    • 您好 Mohebifar,感谢您的回答。我在上面添加了更多代码,希望能更容易地帮助我。只需添加 [] 就不会插入任何选择。感谢您的帮助。
    【解决方案2】:

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

    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');
    

    【讨论】:

    • 您好,感谢您的帮助。一旦我添加了它,它就不会保存任何选择。我是否需要添加另一段代码然后收到多个选择而不是 1?正如我所提到的,我有点菜鸟,所以请和我一起裸露!
    • 显示保存数据的代码。否则,我们将无法为您提供更多帮助。
    • 嗨,我已经在代码的其他部分添加了用户将选择这些框的位置。希望这就是你的意思。再次感谢您的帮助
    • 不,我询问了您 保存 数据的代码(insert 语句)。
    • 我正在尝试自己将代码拼凑在一起,因为我是新手。我意识到这不是一点点,并在上面添加了更多内容。这是 Wordpress 的主题,所以我相信它只会对类别使用原生 wordpress 插入语句,因为主题代码本身没有。 wordress 语句确实允许多项选择,因为我之前在其他网站上使用过它,但是上面的主题代码只允许单项选择。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2014-08-15
    • 2013-10-29
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多