BStud BStud - 7 months ago 12
SQL Question

Populate checked radio buttons from database

I have two sets of radio buttons i.e. two different inputs of upload form. Now I'm trying to make edit page but I'm stuck on populating already checked buttons on them.

First I select information from database

<?php
$sql = "
SELECT *
FROM document_upload
WHERE upload_id = ?
LIMIT 1";

$q = $pdo->prepare($sql);
$q->execute(array($upload_id));
$data = $q->fetch(PDO::FETCH_ASSOC);

$title = $data['upload_title'];
$category = $data['category'];
$skills = $data['skills'];
$description = $data['description'];
?>

<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title" id="title" value="<?php echo !empty($title)?$title:'';?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="description">Description:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="description" id="description" value="<?php echo !empty($description)?$description:'';?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="category">Category:</label>
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=6";
foreach ($pdo->query($sql) as $row_category){
echo '<label class="radio-inline"><input type="radio" name="category" value="'.$row_category['field_name'].'"> '. $row_category['field_name'].'</label>';
}
?>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="skills">Skills:</label>
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=7";
foreach ($pdo->query($sql) as $row_skills){
echo '<label class="radio-inline"><input type="radio" name="skills" value="skills"> '. $row_skills['field_name'].'</label>';
}
?>
</div>


Title and Description are not the problem. They are populated fine.

I can't figured how to select all radio buttons available in database and then check which is selected.

Answer

Use below code to check your radio buttons if value exist.

<div class="form-group">
    <label class="control-label col-sm-2" for="category">Category:</label>  
    <?php
        $pdo = Database::connect();
        $sql="SELECT field_name FROM form_fields WHERE group_id=6";
        foreach ($pdo->query($sql) as $row_category){
            $checked= (isset($category) && $category == $row_category['field_name']) ?  "checked" : "";
            echo '<label class="radio-inline"><input type="radio" name="category" value="'.$row_category['field_name'].'" "'.$checked.'"> '. $row_category['field_name'].'</label>';                               
        }                                               
    ?>                          
</div>

<div class="form-group">
    <label class="control-label col-sm-2" for="skills">Skills:</label>
        <?php
            $pdo = Database::connect();
            $sql="SELECT field_name FROM form_fields WHERE group_id=7";
                foreach ($pdo->query($sql) as $row_skills){
                    $checkedSkill = (isset($skills) && $skills == $row_skills['field_name']) ?  "checked" : "";
                    echo '<label class="radio-inline"><input type="radio" name="skills" value="'.$row_skills['field_name'].'" "'.$checkedSkill.'"> '. $row_skills['field_name'].'</label>';                                
                }                                               
        ?>                      
</div>
Comments