douzhi3586 2013-09-18 00:37
浏览 24
已采纳

在mysql db中使用pdo保存复选框值

I can't save the values in checkbox into my db...always save "array" and not the values..

in fiddler I see that the form catch the values but when the data go to save.php file the values change to array... can you give me a hand with this please?

here is the form:

    <form name="esq" id="esq" method="post">
    <div class="row-fluid grid">
    <div class="span4">
    <label><b><?php echo $translate->__('Gestas Previas'); ?>: </b></label><input type="text" class="span3" value="" name="gestas" /></div>
    <div class="span4">
    <label><b><?php echo $translate->__('Abortos'); ?>: </b></label><input type="text" class="span3" value="" name="abortos" /></div>
    <div class="span4">
    <label><b><?php echo $translate->__('3 espontaneos consecutivos'); ?>: </b></label><input type="text" class="span3" value="" name="esp_conse" /></div>
    </div>

----------HERE START THE CHECKBOX-------------
    <div class="row-fluid grid">
    <label class="control-label"><b><?php echo $translate->__('Ultimo Previo'); ?></b></label>
    <div class="controls">
    <label class="checkbox inline">
    <div id="uniform-inlineCheckbox1" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox1" value="2500g" name="seleccion[]" type="checkbox"></span></div> <?php echo $translate->__('2500 g'); ?>
    </label>
    <label class="checkbox inline">
    <div id="uniform-inlineCheckbox2" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox2" value="4500g" name="seleccion[]" type="checkbox"></span></div> <?php echo $translate->__('4500 g'); ?>
    </label>
    <label class="checkbox inline">
    <div id="uniform-inlineCheckbox3" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox3" value="pre_eclam" name="seleccion[]" type="checkbox"></span></div> <?php echo $translate->__('Preclampsia-eclampsia'); ?>
    </label>
    <label class="checkbox inline">
    <div id="uniform-inlineCheckbox4" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox4" value="cesarea" name="seleccion[]" type="checkbox"></span></div> <?php echo $translate->__('Cesárea'); ?>
    </label>
    </div>
    </div><br />
----------HERE FINISH THE CHECKBOX-------------
..............

here is the code:

<?php 
include_once("confs.php");
if (!empty($_POST)) {
        try{
    $statement = $conn->prepare("INSERT INTO GESTACION (gestas, abortos, esp_conse, seleccion, partos, vaginal, cesareas, nac_vivos, nac_muertos, viven, semana, des_semana, f_ult_parto, f_ult_pap, fur, fpp, edad_gesta, pesofeto, tallafeto, imc, toxoide, influenza, otras, medicament) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    if ($statement->execute(array($_POST['gestas'], $_POST['abortos'], $_POST['esp_conse'], $_POST['seleccion'], $_POST['partos'], $_POST['vaginal'], $_POST['cesareas'], $_POST['nac_vivos'], $_POST['nac_muertos'], $_POST['viven'], $_POST['semana'], $_POST['des_semana'], $_POST['f_ult_parto'], $_POST['f_ult_pap'], $_POST['fur'], $_POST['fpp'], $_POST['edad_gesta'], $_POST['pesofeto'], $_POST['tallafeto'], $_POST['imc'], $_POST['toxoide'], $_POST['influenza'], $_POST['otras'], $_POST['medicament'])));
        $dbSuccess = true;     
} catch (Exception $e) {
    $return['databaseException'] = $e->getMessage();
}
   $dbh = null;  
}
?>

all other data save nice into db.

  • 写回答

1条回答 默认 最新

  • du0204 2013-09-18 00:56
    关注

    Given the extra information in your comments, I'd say you simply want this

    $seleccion = is_array($_POST['seleccion'])
        ? implode(', ', $_POST['seleccion'])
        : $_POST['seleccion'];
    
    // snip
    
    $statement->execute(array(
        $_POST['gestas'],
        $_POST['abortos'],
        $_POST['esp_conse'],
        $seleccion, ...
    

    This will save any checkbox selections as a comma separated list with the GESTACION table row

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能