无法使用codeigniter将使用复选框的数据存储到数据库

xbp102n0  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(130)

我尝试使用复选框存储一些数据到我的数据库,但它不能存储。下面是我的代码:
我尝试使用复选框存储一些数据到我的数据库,但它不能存储。下面是我的代码:
我尝试使用复选框存储一些数据到我的数据库,但它不能存储。下面是我的代码:
查看:

<form method="post" action="<?php echo base_url().'igd/igd/input_airborne'?>">
                                <?php echo $this->session->flashdata('message_airborne');?>
                                <input type="text" class="form-control" name="KUNJUNGAN" value="<?php echo $igd->NOMOR ?>" readonly>
                                <input type="datetime-local" class="form-control" name="TANGGAL" value="<?php echo date("Y-m-d H:i:s") ?>">
                                <input type="text" class="form-control" name="OLEH" value="<?php echo $session_user->nip?>" readonly>
                                <input type="text" class="form-control" name="STATUS" value="1" readonly>

<div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
                                    <label class="form-check-label" for="defaultCheck1">
                                        TBC AKtif
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck2">
                                    <label class="form-check-label" for="defaultCheck2">
                                        Campak
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
                                    <label class="form-check-label" for="defaultCheck1">
                                        MDR TB
                                    </label>
                                </div> 
<button class="btn btn-sm btn-success mt-1 mb-1"> Masukkan</button>
                                </form>

控制器:

public function input_airborne(){
        $DESKRIPSI = $this->input->post('DESKRIPSI');
        $TANGGAL = $this->input->post('TANGGAL');
        $OLEH = $this->input->post('OLEH');
        $STATUS = $this->input->post('STATUS');
        $KUNJUNGAN = $this->input->post('KUNJUNGAN');

        $data = array(
            'DESKRIPSI' => $DESKRIPSI,
            'TANGGAL' => $TANGGAL,
            'OLEH' => $OLEH,
            'STATUS' => $STATUS,
            'KUNJUNGAN' => $KUNJUNGAN
        );
        $this->M_Igd->input_airborne($data, 'igd_inim_airborne');
        $this->session->set_flashdata('message_airborne','<div class="alert alert-success alert-dismissible mt-1 fade show" role="alert">
                                                Airborne berhasil dimasukkan
                                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                                </button>
                                                </div>');
        redirect($_SERVER['HTTP_REFERER']);

    }

DESKRIPSI字段用于存储我复选框条目

4c8rllxm

4c8rllxm1#

您应指定:-

<input type="checkbox" name="Days[]" value="Daily">Daily<br>

[]添加到所有names天中,并在php中使用该数组。
之后,可以在db的不同列插入值,或者使用implode将值保存到一列中。

<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]" value="Monday">Monday<br>
<input type="checkbox" name="Days[]" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]" value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]" value="Friday">Friday<br>
<input type="checkbox" name="Days[]" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

在PHP服务器端脚本:-

$checkBox = implode(',', $_POST['Days']);
nukf8bse

nukf8bse2#

已经解决了,我只是改变

$DESKRIPSI = $this->input->post('DESKRIPSI');

$DESKRIPSI = implode(', ', $_POST['DESKRIPSI']);

别忘了把名字改成

name="DESKRIPSI[]";

因为数据将存储为数组

相关问题