doutanghuan9595 2012-03-09 04:27
浏览 53
已采纳

插入表单复选框PHP用逗号分隔 - 检索它们

I was looking in stackoverflow, but i do not find exactly what I need. I need to insert checkboxes data sent with php into mysql like this 22111,22332,12123,121132.

I have this code (found here in the site)

<?php 
foreach($_POST['idecod'] as $check) {
            $sel = $check.',';}

$contenedor = "UPDATE inmdes SET idecod='$sel'";

$insertdb = mysql_query($contenedor) or die(mysql_error());

?>

Of course I have the checkboxes as an array:

<input name="idecod[]" type="checkbox" value="<?php echo $rs[0]; ?>"  />

How can i insert multiple checkboxes like 2211223,2211223,1212233,12332122 or if is just one 222111222..???

And how can i retrive them as an array because i need to use something like this

<? php if (in_array('2211223', $idecod)) echo "checked"; ?>

To automatically check the previous checked boxes.!

Thanks in advance

Roberto

UPDATE

I have a list of options. These options act/des with checkboxes. I need to:

1.- Be able to register options checked (SOLVED thanks to @prodigitalso) 2.- Be able to automatically check the checkboxes previously checked.

So, I have the codes like this 221122,221133,225566,445522 in the table (mysql). I need the script to check what codes where previously checked (those that are in the table) and check them. For example:

I see the 5 options 221122,221133,225566,445522 and 663322. I check only 4 (221122,221133,225566,445522) and this is UPDATED in mysql database.

I come again to the options page. But previously i checked 221122,221133,225566,445522. So the script check EACH checkbox. So automatically check options 221122,221133,225566,445522 BUT it doesnt check 663322.

RS[0] is the code, for example 221122.

  • 写回答

2条回答 默认 最新

  • duancenxie2233 2012-03-09 04:33
    关注

    You should use explode and implode.

    if(isset($_POST['idecod']) && !empty($_POST['idecod'])) {
       $sel = implode(',', $_POST['idecod']);
       $contenedor = sprintf("UPDATE inmdes SET idecod='%s'", mysql_real_escape_string($sel));
       $insertdb = mysql_query($contenedor) or die(mysql_error());
    }
    

    then when you pull them out just use:

    $idecodArr = explode(',', $row['idecod']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?