doufei4418 2016-08-30 16:46
浏览 45
已采纳

php mysql如何检查多个复选框添加/删除选项和更​​新数据库

I have 3 tables - product, category, product_category.

Table product -> PID , product name

Table category -> CID, category name

Table product_category -> PID, CID

my problem is handling product_category table. Let me explain it in details.

When I add new product, I need to update 2 tables -> product and product_category.

product_category is used to store the product ID and category ID as 1 product can be assigned to multiple categories.

When adding new product, 1 input box plus 1 array to display the categories for user to choose. For example.

foreach($display->category_name as $iid => $name) {

            echo '

                <div class="checkbox">
                    <label><input type="checkbox" name="check_list[]" value="'.$iid.'">'.$name.'</label>
                </div>

                ';
        }

So when clicking on update button, it will show the records that user has keyed in earlier.

enter image description here

So my question is how to check if user has check 1 more option OR uncheck 1 or more option OR remove all of them. And then update table product_category accordingly.

Refer to the screenshot

Case 1 : (this one is easy, just add 1 more option)

Before->Western food and Mix food are checked.

After->Western food , Mix food + Chinese food

Case 2:(remove Western food)

Before->Western food and Mix food are checked.

After->Mix food is checked only.

Case 3:(remove Western food and add Chinese food)

Before->Western food and Mix food are checked.

After->Mix food and Chinese food are checked.

  • 写回答

2条回答 默认 最新

  • duanji7881 2016-08-30 17:43
    关注

    Use array_diff() to get list of unchecked values:

    $old_check_list = ['1', '2'];
    $new_check_list = ['2', '3'];
    $unchecked_values = array_values(array_diff($old_check_list, $new_check_list));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧