0

我有一个带有从表中加载的复选框组的表单。表字段是 MySQL TINYINT 字段,其中 1 表示真,0 表示假,因此命名为:frequency_sun, frequency_mon, frequency_tue, frequency_wed, frequency_thu, frequency_fri, frequency_sat

表单是这样设置的:

    <?php
$checked = ($frequency_sun==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[sun]' value=".$frequency_sun." >Sunday";
$checked = ($frequency_mon==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[mon]' value=".$frequency_mon." >Monday";
$checked = ($frequency_tue==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[tue]' value=".$frequency_tue." >Tuesday";
$checked = ($frequency_wed==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[wed]' value=".$frequency_wed." >Wednesday";
$checked = ($frequency_thu==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[thu]' value=".$frequency_thu." >Thursday";
$checked = ($frequency_fri==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[fri]' value=".$frequency_fri." >Friday";
$checked = ($frequency_sat==1) ? 'checked="checked"' : '';
                    echo "<input type=\"checkbox\"" . $checked . "name='frequency[sat]' value=".$frequency_sat." >Saturday";
                    ?>

我怀疑其中一部分是不必要的和/或笨拙的,但这不是我的主要问题——至少不是马上。无论如何,复选框组正在正确加载。我试图通过为数据设置一个变量来检索表单$_POST数据:

$frequency = $_POST['frequency'];

// 然后我设置要在 SQL UPDATE 中使用的变量,如下所示:

$frequency_sun = (isset($frequency['sun']) ? $frequency['sun'] : $frequency_sun);
$frequency_mon = (isset($frequency['mon']) ? $frequency['mon'] : $frequency_mon);
$frequency_tue = (isset($frequency['tue']) ? $frequency['tue'] : $frequency_tue);
$frequency_wed = (isset($frequency['wed']) ? $frequency['wed'] : $frequency_wed);
$frequency_thu = (isset($frequency['thu']) ? $frequency['thu'] : $frequency_thu);
$frequency_fri = (isset($frequency['fri']) ? $frequency['fri'] : $frequency_fri);
$frequency_sat = (isset($frequency['sat']) ? $frequency['sat'] : $frequency_sat);

选中特定复选框时,它似乎没有返回值。对于我的测试,我已frequency_wed在表中设置为 0 并希望将其设置为 1,但我不知道哪里出错了。

4

1 回答 1

0

为了保持你的编码方式,你可以做这样的事情

<?
    if($frequency_sun == 1) $checked_sun = 'checked="checked"';
    if($frequency_mon == 1) $checked_mon = 'checked="checked"';
    if($frequency_tue == 1) $checked_tue = 'checked="checked"';
    if($frequency_wed == 1) $checked_wed = 'checked="checked"';
    if($frequency_thu == 1) $checked_thu = 'checked="checked"';
    if($frequency_fri == 1) $checked_fri = 'checked="checked"';
    if($frequency_sat == 1) $checked_sat = 'checked="checked"';
?>    

    <input type="chexkbox" name='frequency[sun]' value="<?=$frequency_sun?>" <?=$checked_sun?> />Sunday<br />
    <input type="chexkbox" name='frequency[mon]' value="<?=$frequency_mon?>" <?=$checked_mon?> />Monday<br />
    <input type="chexkbox" name='frequency[tue]' value="<?=$frequency_tue?>" <?=$checked_tue?> />Tuesday<br />
    <input type="chexkbox" name='frequency[wed]' value="<?=$frequency_wed?>" <?=$checked_wed?> />Wednesday<br />
    <input type="chexkbox" name='frequency[thu]' value="<?=$frequency_thu?>" <?=$checked_thu?> />Thursday<br />
    <input type="chexkbox" name='frequency[fri]' value="<?=$frequency_fri?>" <?=$checked_fri?> />Friday<br />
    <input type="chexkbox" name='frequency[sat]' value="<?=$frequency_sat?>" <?=$checked_sat?> />Saturday<br />

接下来,您需要从频率数组中获取值

$frequency = $_POST['frequency'];
$frequency_sun = $frequency['sun'];
"UPDATE yourtable SET sunday = '$frequency_sun', ..... WHERE something = 'value'";

然后你选择

"SELECT * FROM yourtable WHERE something = 'value'"

最后是你的价值观

$frequency_sun = $query_result['sunday'];
.
.
.

当然,您需要在表单之前从数据库中获取值。

于 2013-06-24T03:26:44.233 回答