在选项上显示数据库值

时间:2015-09-29 03:25:00

标签: php html jquery-mobile html-select

我试图在用户可以更新其他选项之前在数据库中显示已保存的数据。我不确定如何编码。任何人都可以提出可能的解决方案?已经尝试了很多方法,但无法这样做。

更新:我发现了选项未显示的原因。

这是我的代码:

`
     

$consentId = $_GET['consent_id'];

$retrieveConsent = "SELECT * FROM consent, leavetype WHERE consent.type_of_leave = leavetype.type_of_leave";
$retrieveResult = mysqli_query($link, $retrieveConsent) or die("Retrieve Error" . mysqli_error($link));

$queryleavetype = "SELECT * FROM leavetype";
$queryleaveresult = mysqli_query($link, $queryleavetype) or die("Leave Retrieve Error " . mysqli_error($link));

$row = mysqli_fetch_array($retrieveResult);


mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Edit Consent </title>
</head>
<body>
            <div class="ui-content">
                <h3><b>Edit Leave</b></h3>
                <form action="doEditConsent.php" method="post" data-ajax="false">

                    <input type="hidden" name="cId" value="<?php echo $row['consent_id']; ?>"/>

                    <label for="dateFrom" ><b>Date From:</b></label>
                    <input type="date" id="dateFrom" name="newDateFrom" value="<?php echo $row['consent_date_from']; ?>" required>
                    <br>

                    <label for="dateTo" ><b>Date To:</b></label>
                    <input type="date" id="dateTo" name="newDateTo" value="<?php echo $row['consent_date_to']; ?>" required>
                    <br>

                    <label for="reason" ><b>Leave Type:</b></label>
                    <select name="leaveType" id="leaveType" data-mini="true">
                        <?php
                        while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
                            ?>
                            <option value="<?php echo $rowleave['type_of_leave']; ?>">
                                <?php echo $rowleave['leave_type']; ?>
                            </option>
                            <?php
                        };
                        ?>
                    </select>
                    <br>

                    <button class="ui-btn ui-corner-all" type="submit" >Submit</button>
                </form>
            </div>
        <?php
        }
    }
    ?>
</body>
</html>`

1 个答案:

答案 0 :(得分:0)

试试这个,希望它会对你有所帮助:

<select name="leaveType" id="leaveType" data-mini="true">
    <option value=""></option>
    <?php
    $previous_selected_value = 'previous_selected_value';//get the previous value and assign it to this variable
    while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
        $selected = ($rowleave['type_of_leave'] == $previous_selected_value) ? " selected='selected'" : "";
        echo '<option value="'.$rowleave['type_of_leave'].'"'.$selected.'>'.$rowleave['leave_type'].'</option>';
    }
    ?>
</select>