php - 从 <select multiple> 框中将多个选择输入数据库

标签 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);

最佳答案

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

关于php - 从 <select multiple> 框中将多个选择输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20034675/

相关文章:

mysql - 获取我在 SQL 中关注的用户的关注

sql - 根据条件选择相同列作为不同列

在 "index"的情况下,PHP $_GET 无法获取 RewriteRule 的值

php - 如何将此字符串反序列化为键=>值对的PHP数组?

PHP:使用正则表达式从 IP 地址中删除小数

sql - 使用单个 SQL 语句检索两个 COUNT

mysql - Grails:如何减少数据库查询

php - 使用 PDO 将输入数组插入 mysql

PHP/MYSQL - 使用 mysql_insert_id 插入多行?

MySql select,获取价格*数量的小计,具有多个数量标准