从数据库发送值到复选框已选中

时间:2015-08-31 11:02:11

标签: php html pdo

我有一个包含3个字段looking_iduser_idlooking的表格。 looking_id是独一无二的。

$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=$dbg->query($sql);
<input type="checkbox" name="What_services[]" id="What_services" value="person_care" <?php if($stmt=='person_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="child_care" <?php if($stmt=='child_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="pet_care" <?php if($stmt=='pet_care')echo checked;?> >

但它不起作用。

2 个答案:

答案 0 :(得分:1)

试试这个

$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=mysql_query($sql);
while($result = mysql_fetch_array($stmt))
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $result['looking'];?>" value="person_care" <?php if($result['looking']=='person_care')echo "checked";?> >
<?php
} ?>

答案 1 :(得分:0)

对于pdo试试这个

$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
foreach ($stmt->query($sql) as $row)
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $row['looking'];?>" value="person_care" <?php if($row['looking']=='person_care')echo "checked";?> >
<?php
} ?>