使用动态字段名称在mysql中保存数据

时间:2016-08-08 19:19:22

标签: php mysql

我有mysql表存储公司ID,开放/关闭时间为星期几和状态(关闭/打开)。制作带字段和更新功能的表单。看起来像这样。工作,但我相信它可以优化。你能建议怎么做?

FORM:

            <form role="form" method="post" action="" autocomplete="off">
                <div class="box-body">
                    <div class="row">
                        <div class="col-sm-3 text-bold"><div class="padd bg-gray-light">Day</div></div>
                        <div class="col-sm-3 text-bold"><div class="padd bg-gray-light">Status</div></div>
                        <div class="col-sm-3 text-bold"><div class="padd bg-gray-light">Opening</div></div>
                        <div class="col-sm-3 text-bold"><div class="padd bg-gray-light">Closing</div></div>
                    </div>
                    <?php while ($h = dbarray($get_hours)) { ?>
                    <input type="text" name="day" value="<?php echo $h['day_day']; ?>">
                    <?php
                    // FORM VALUES
                    if ($h['day_status'] == 1) {
                        $status_o = "checked";
                        $status_c = "";
                    }
                    if ($h['day_status'] == 0) {
                        $status_o = "";
                        $status_c = "checked";
                    }
                    ?>
                    <div class="row">
                        <div class="col-sm-3">
                            <h4><?php echo $l['day_'.$h[day_day]]; ?></h4>
                        </div>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <div class="checkbox">
                                    <label><input type="radio" name="status_<?php echo $h['day_day']; ?>" value="1" <?php echo $status_o; ?>> Dirbame</label>
                                    <label><input type="radio" name="status_<?php echo $h['day_day']; ?>" value="0" <?php echo $status_c; ?>> Uždaryta</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <input type="text" name="open_<?php echo $h['day_day']; ?>" class="form-control <?php if ($h['day_status'] == 0) { echo "bg-gray"; } ?>" value="<?php echo $h['day_open']; ?>">
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <input type="text" name="close_<?php echo $h['day_day']; ?>" class="form-control <?php if ($h['day_status'] == 0) { echo "bg-gray"; } ?>" value="<?php echo $h['day_close']; ?>">
                            </div>
                        </div>
                    </div>
                    <hr style="margin: 3px 0;">
                    <?php } ?>
                </div>

                <div class="box-footer">
                    <button type="submit" name="save" class="btn btn-success pull-right">Save</button>
                </div>
            </form>

更新PHP:

<?php
if (isset($_POST['save'])) {
    // VALUES
    $mon_status = stripinput(descript($_POST['status_1'])); $mon_open = stripinput(descript($_POST['open_1'])); $mon_close = stripinput(descript($_POST['close_1']));
    $tue_status = stripinput(descript($_POST['status_2'])); $tue_open = stripinput(descript($_POST['open_2'])); $tue_close = stripinput(descript($_POST['close_2']));
    $wed_status = stripinput(descript($_POST['status_3'])); $wed_open = stripinput(descript($_POST['open_3'])); $wed_close = stripinput(descript($_POST['close_3']));
    $thu_status = stripinput(descript($_POST['status_4'])); $thu_open = stripinput(descript($_POST['open_4'])); $thu_close = stripinput(descript($_POST['close_4']));
    $fri_status = stripinput(descript($_POST['status_5'])); $fri_open = stripinput(descript($_POST['open_5'])); $fri_close = stripinput(descript($_POST['close_5']));
    $sat_status = stripinput(descript($_POST['status_6'])); $sat_open = stripinput(descript($_POST['open_6'])); $sat_close = stripinput(descript($_POST['close_6']));
    $sun_status = stripinput(descript($_POST['status_7'])); $sun_open = stripinput(descript($_POST['open_7'])); $sun_close = stripinput(descript($_POST['close_7']));
    // UPDATE
    $result1 = dbquery("UPDATE a_days SET day_status='".$mon_status."', day_open='".$mon_open."', day_close='".$mon_close."' WHERE day_day=1");
    $result2 = dbquery("UPDATE a_days SET day_status='".$tue_status."', day_open='".$tue_open."', day_close='".$tue_close."' WHERE day_day=2");
    $result3 = dbquery("UPDATE a_days SET day_status='".$wed_status."', day_open='".$wed_open."', day_close='".$wed_close."' WHERE day_day=3");
    $result4 = dbquery("UPDATE a_days SET day_status='".$thu_status."', day_open='".$thu_open."', day_close='".$thu_close."' WHERE day_day=4");
    $result5 = dbquery("UPDATE a_days SET day_status='".$fri_status."', day_open='".$fri_open."', day_close='".$fri_close."' WHERE day_day=5");
    $result6 = dbquery("UPDATE a_days SET day_status='".$sat_status."', day_open='".$sat_open."', day_close='".$sat_close."' WHERE day_day=6");
    $result7 = dbquery("UPDATE a_days SET day_status='".$sun_status."', day_open='".$sun_open."', day_close='".$sun_close."' WHERE day_day=7");
}
?>

1 个答案:

答案 0 :(得分:0)

循环遍历字段名称的数字后缀。

$results = array();
foreach (range(1, 7) AS $daynum) {
    $status = stripinput(descript($_POST['status_' . $daynum]));
    $open = stripinput(descript($_POST['open_' . $daynum]));
    $close = stripinput(descript($_POST['close_' . $daynum]));
    $results[$daynum] = dbquery("UPDATE a_days SET day_status='".$status."', day_open='".$open."', day_close='".$close."' WHERE day_day='.$daynum);
}