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 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据