如何使用复选框检索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
但是未在复选框中显示已选中...错误在哪里?
最佳答案
您正在构造一个准备好的语句,并(直接)传递一个变量。那是不对的。在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
。这应该解决它。
关于php - 使用pdo检索mysql中的复选框数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26412008/