-2

I have a table with 3 fields looking_id,user_id,looking. looking_id is unique.

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

but it's not working.

5
  • 1
    "but it's not working." What isn't working?
    – Epodax
    Aug 31, 2015 at 11:04
  • Which table column holds those values? Aug 31, 2015 at 11:04
  • 1
    $stmt is not a single value, but an array. Try parsing $stmt['looking_id'] (I guess that this is the column you want).
    – user2480047
    Aug 31, 2015 at 11:05
  • 1
    You need to do foreach ($stmt->query($sql) as $row) { print $row['looking_id'] . "\t"; print $row['user_id'] . "\t";
    – Saty
    Aug 31, 2015 at 11:06
  • Separate PHP from HTML and don't use identical input id names, they are supposed to be unique.
    – DeDee
    Aug 31, 2015 at 11:07

2 Answers 2

1

Try this

$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
  • OP use pdo not mysql you can also check the tag in question!! FYI mysql is deprecated
    – Saty
    Aug 31, 2015 at 11:09
0

For pdo try this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.