duanke1286 2019-02-28 15:39
浏览 34
已采纳

我想将多个选择数据发送到同一列

Hy, I want to insert different data to the same column in the database. let see the Picture first. if the user can check the New Product? checkbox. then I want this can be stored in the product_label column of the database with a value of new. Now if someone clicks the Product on Sale? checkbox then I want to send the value sale to the database column product_label. and if the user cannot be checked the both of the checkboxes then I want to send null to the product_label column of the database.

enter image description here

Now here is the HTML code for that. I just show only the checkboxes from the HTML code because the rest of the code is too lengthy.

<div class="col-md-4">
  <div class="form-group">
    <label for="Sale">New Product?</label>
        <input type="checkbox" id="new_product" name="new_product" class="checkbox checkboxsize">
    </div>
 </div>
   <div class="col-md-4">
      <div class="form-group">
       <label for="Sale">Product On Sale?</label>
          <input type="checkbox" id="sale" name="sale" class="checkbox checkboxsize">
        </div>
</div>

and this is PHP code for insertion.

    $new_product = $_POST['new_product'];
        $label = $_POST['sale']; 
        if($label==true)
            {
                 $label="sale";
            }
        else if($new_product==true){
                $new_product ="new";
        }
        else{

        }
$query = "INSERT INTO `products`(`product_title`, `product_img1`, `product_img2`, `product_img3`, `product_price`, `product_desc`, `product_keywords`,`product_size`, `product_color`,`product_label`) VALUES ('$product_category','$category','$title','$path1','$path2','$path3','$price','$description','$keywords','$product_size','$product_color','$label')";

Now here I am stuck with this code. I didn't know how to get and insert these value to the same column product_label

  • 写回答

2条回答 默认 最新

  • douchong8393 2019-02-28 15:51
    关注

    When a checkbox is not checked the name of the input doesn't exist in $_POST array, you have to use isset().

    So try this :

    <?php
    
    if(isset($_POST['sale']))
    {
        $label = "sale";
    }
    else if(isset($_POST['new_product'])) {
        $label = "new";
    }
    else{
        $label = '';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计