填充数据库中选中的单选按钮

时间:2016-04-18 08:40:24

标签: php mysql

我有两组单选按钮,即两种不同的上传形式输入。现在我正在尝试制作编辑页面,但我仍然坚持填充已经检查过的按钮。

首先,我从数据库中选择信息

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

标题和说明不是问题。他们人口充足。

我无法想出如何选择数据库中可用的所有单选按钮,然后检查选择了哪个。

1 个答案:

答案 0 :(得分:0)

如果存在值,请使用以下代码检查单选按钮。

<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>