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条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择