【问题标题】:Retrieve checkbox data in mysql with pdo使用 pdo 检索 mysql 中的复选框数据
【发布时间】:2014-12-12 06:27:29
【问题描述】:

如何使用复选框检索以前保存在 mysql 中的数据以显示保存的内容?这是我的表格:

<?php
    try {
        $query = "select * from CONSULTA where user_id = '$user_id'";
        $stmt = $conn->prepare( $query );
        $stmt->execute();
        while($row = $stmt->fetch()){
                $sick = $row['sick'];
                $user_id = $row['user_id'];
        }
    }catch(PDOException $exception){ 
        echo "Error: " . $exception->getMessage();
    }
?>

<form name="histo" id="histo" method="post">
    <div class="row-fluid grid">
        <label class="control-label"><b><?php echo $translate->__('Do you have any of the following symptoms'); ?>? :</b></label>
        <div class="controls">
            <label class="checkbox inline">
                <div id="uniform-inlineCheckbox1" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox1" value="1" name="sick[]" type="checkbox"<?php if ($sick == '1') {echo ' checked="checked"';} ?>></span></div> <?php echo $translate->__('Arrhythmias'); ?>
            </label>
            <label class="checkbox inline">
                <div id="uniform-inlineCheckbox2" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox2" value="2" name="sick[]" type="checkbox"<?php if ($sick == '2') {echo ' checked="checked"';} ?>></span></div> <?php echo $translate->__('Heart murmur'); ?>
            </label>
            <label class="checkbox inline">
                <div id="uniform-inlineCheckbox3" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox3" value="3" name="sick[]" type="checkbox"<?php if ($sick == '3') {echo ' checked="checked"';} ?>></span></div> <?php echo $translate->__('Stroke'); ?>
            </label>
            <label class="checkbox inline">
                <div id="uniform-inlineCheckbox3" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox4" value="4" name="sick[]" type="checkbox"<?php if ($sick == '4') {echo ' checked="checked"';} ?>></span></div> <?php echo $translate->__('Angina'); ?>
            </label>
            <label class="checkbox inline">
                <div id="uniform-inlineCheckbox3" class="checker"><span><input style="opacity: 0;" id="inlineCheckbox5" value="5" name="sick[]" type="checkbox"<?php if ($sick == '5') {echo ' checked="checked"';} ?>></span></div> <?php echo $translate->__('Other'); ?>
            </label>
        </div>
    </div>
    <input type="hidden" name="user_id" value="<?php echo $_GET[user_id]; ?>" />
    <?php if (!empty($visit)) { echo '<input type="hidden" name="action" value="edit" />'; } else { echo '<input type="hidden" name="action" value="crear" />';} ?>

    <input type='submit' class='btn btn-primary' value='<?php $translate->__('Save History'); ?>' />
</form>
<div id="loading4" style="display:none;"><img src="img/ajax-loaders/loading4.gif" /></div>
<div id="oh"></div>

这是保存到数据库中的数据:

sick  1, 3, 5

但未显示复选框中的选中...错误在哪里?

【问题讨论】:

  • $sick的值是多少?
  • 您应该使用named parameters 准备语句,而不是仅仅填充任意用户数据。 $user_id 之类的内容不应直接出现在您的查询中。
  • @u_mulder DB 中的值是这些= 1, 3, 5

标签: php mysql checkbox pdo


【解决方案1】:

您正在构建一个准备好的语句并(直接)传入一个变量。这是不正确的。比如在PDO中你要么使用?要么:user_id,那么我们必须在准备好语句后绑定参数,请注意:

$query = "select * from CONSULTA where user_id = ?";
$stmt = $conn->prepare( $query );
$stmt->bindParam(1, $user_id, PDO::PARAM_INT);
$stmt->execute();

这将使您的查询正确。但是,当您显示1,3,5 时,我看不到您的数据是如何存储的。如果是这种情况,那么这是一个 CSV 字符串,您需要对其进行分解然后进行比较。

$sick = explode(',', $row['sick']);

然后你需要改变你的条件语句。对于本例,我将使用三元运算符。

<?php echo in_array(1, $sick)? 'checked="checked"': ''; ?>

根据您的代码行,将上述行中的 1 替换为 2,3,4,5

这应该可以解决它。

【讨论】:

  • 我这里有一个错误Parse error: syntax error, unexpected ''; ?&gt;&gt;&lt;/span&gt;&lt;/div&gt; &lt;?php echo' (T_CONSTANT_ENCAPSED_STRING),我把这部分$sick = explode(',', $sick);改成这个=$sick = explode(',', $row['sick']);
  • @joaquin 请用我的代码再试一次,我省略了'
  • 再次嗨,不要显示选中复选框中的检查...我把这个 &lt;?php echo $sick; ?&gt; 给我看 Array...你能帮帮我吗
  • @joaquin 你需要三元运算符&lt;?php echo in_array(1, $sick)? 'checked="checked"': ''; ?&gt;,而不是&lt;?php echo $sick; ?&gt;
  • 是的,我知道,我像你说的那样插入了,但没有显示复选框 1、3、5 框中的检查...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
相关资源
最近更新 更多