shire
shire

Reputation: 1

How could I display a checked checkbox based from it's value in the database?

I'm new in PHP, could you help me solve my problem? I'm trying to display the checkbox checked based on its value in the database. I have saved its value as 1 if it's checked and 0 if it's not.

Upvotes: 0

Views: 21730

Answers (2)

Dan Grossman
Dan Grossman

Reputation: 52372

<?php

$sql = "SELECT somecol FROM sometable";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$checked = $result['somecol'];

?>

<input type="checkbox" name="somecol" value="1" <?php if ($checked == 1) echo 'checked'; ?> />

Upvotes: 7

RichardTheKiwi
RichardTheKiwi

Reputation: 107706

You can test the field, let's say $row['col'], and emit checked="checked" if the field contains 1.

echo '<input type="checkbox" name="n" value="v"' . ($row['col']==1 ? ' checked="checked"' : '') . '>';

Upvotes: 4

Related Questions