【问题标题】:insert multi value from checkboxs into database by php通过php将复选框中的多个值插入数据库
【发布时间】:2011-06-18 17:08:24
【问题描述】:

我想将此表单值插入数据库:

<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>

这些文本框是通过 php 从数据库中的表中获取的,可能是变量

我想以这种格式插入数据库 如果品牌 1 被选中 $brand="1,";

最后像这样:

insert($name,$brands); and $brands = "1,2,3,4,5,";

如果用 if 和 while 写这个,但它不起作用,因为 if insert run in while {} 五次 insert Done 和 if insert run out of while {} , $brand = "5,"

感谢您对此问题的帮助或想法

意思是:

<form method="post" action="#">
<?php
$result = $db->getall(brands);
        if(!empty($result)) {
    while ( list($key,$val)=each($result) ) {
        $brand_id = stripslashes($val["id"]);
        $brand_name = stripslashes($val["name"]);
    ?>
    <input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
    <?php }} ?>

源输出:

<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>

提交表单时,插入源为:

    <?php
$result = $db->getall(brands);
        if(!empty($result)) {
    while ( list($key,$val)=each($result) ) {
        $brand_id = brand.stripslashes($val["id"]);
        $brand_name = stripslashes($val["name"]);
    $brand_ids = "brand.$brand_id";
    if($$brand_ids==1) {$brands="$brandid,"}

}} ?>
$db->add_submenu("$brands"); 

【问题讨论】:

  • 我认为您应该尝试编辑您的问题并再次解释所有内容,因为很难理解您要如何插入以及您的问题是什么。
  • $db-&gt;add_submenu("$brands"); 在 PHP 块之外。

标签: php database checkbox insert


【解决方案1】:

您应该将复选框的名称更改为brand[]。在$_POST['brand'] 提交后,它将为您提供一个数组

Ex. 

<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />

另一方面,您可以执行以下操作:

// this will return '1, 2, 3, 4, 5' when all are selected. 
$index = implode(", ", $_POST['brand']); 

此时您将拥有逗号分隔形式的品牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多