gpt4 book ai didi

php - 在 mysql 数据库中使用 pdo 保存复选框值

转载 作者:行者123 更新时间:2023-11-30 23:13:39 24 4
gpt4 key购买 nike

我无法将复选框中的值保存到我的数据库中...总是保存“数组”而不是值..

在 fiddler 中,我看到表单捕获了值,但是当数据转到 save.php 文件时,值更改为数组...请您帮我解决这个问题吗?

这是表格:

    <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-------------
..............

代码如下:

<?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;
}
?>

所有其他数据都很好地保存到数据库中。

最佳答案

鉴于您评论中的额外信息,我想说您只是想要这个

$seleccion = is_array($_POST['seleccion'])
? implode(', ', $_POST['seleccion'])
: $_POST['seleccion'];

// snip

$statement->execute(array(
$_POST['gestas'],
$_POST['abortos'],
$_POST['esp_conse'],
$seleccion, ...

这会将所有复选框选择保存为逗号分隔的列表,其中包含 GESTACION 表行

关于php - 在 mysql 数据库中使用 pdo 保存复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18862060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com