簡體   English   中英

使用單個復選框與php mysql在一行中插入多個記錄

[英]inserting multiple records in one row using single checkbox with php mysql

我在使用php mysql的單個復選框將多條記錄插入一行中時感到困惑

這是我的表格表

<form action="tambahMatkul.php" method="post" id="ambil_matkul">  
        <table class="tabel table-bordered table-stripped table-responsive">

        <tr>
        <th width="5%">cek</th>
        <th width="19%">Kode</th>
        <th width="19%">Mata Kuliah</th>
        <th width="19%">W/P</th>
        <th width="19%">SKS</th>
        <th width="19%">Kelas</th>
        </tr>
        <?php 
            include "koneksi.php";

            $query = $connect->query("SELECT * FROM matakuliah")or die(mysqli_error($connect));
            while($mahasiswa = $query->fetch_array())
            {
            ?>
                <tr>
                    <td> <input type="checkbox" name="cek[]" value="<?php echo $mahasiswa['kodeMatkul'] ?>"> 
                         <
                    </td>
                    <td><?php echo $mahasiswa['kodeMatkul']; ?></td>
                    <td><?php echo $mahasiswa['namaMatkul']; ?></td>
                    <td><?php echo $mahasiswa['pilihan']; ?></td>
                    <td><?php echo $mahasiswa['sks']; ?></td>
                    <td><?php echo $mahasiswa['kelas']; ?></td>
                    </input>
                </tr>
            <?php } ?>
        </table>
        <hr/>
        <div class="btn-group">
            <button class="btn btn-sm btn-success" type="submit"><i class="fa fa-plus"></i> Tambah</button>
        </div>
    </form>

這是我的形式動作

<?php

$codes = $_POST['cek'];

foreach($codes as $code)
{
    $connect = mysqli_connect("localhost","root","","krs");
    $query = mysqli_query($connect, "INSERT INTO krs VALUES(NULL, '161402133', '{$code}')");
}
if($query)
echo "Matakuliah sudah ditambahkan.";

我不知道該怎么辦? 還是初學者。 請高手幫我

您需要循環和數組以獲取多個復選框值。 以下是示例

 $a=array();
 foreach($_POST['cek'] as $value) {
        $a[]=$value;
 }

我也將參考此POST以獲取更多詳細信息

帖子作者姓名: sean-walsh

您的代碼中還有其他錯誤:

1:刪除循環中的數據庫連接

我建議使用此代碼將這些值插入數據庫

$a=array();
     foreach($_POST['cek'] as $value) {
            $a[]=$value;
     }
$checkData = implode(",", $a);
    $connect = mysqli_connect("localhost","root","","krs");
    $query = mysqli_query($connect, "INSERT INTO krs VALUES(NULL, '161402133', ".$checkData.")");

 mysqli_close($con);

您需要處理POST值,並將它們保存在一個查詢中。

例如:

$const = 161402133;
$values = "('{$const}'," . implode("),('{$const}',", $_POST['cek']) . ")";

$connect = mysqli_connect("localhost", "root", "", "krs");
$query = mysqli_query($connect, "INSERT INTO krs VALUES {$values}");

if ($query) {
            echo "Matakuliah sudah ditambahkan.";
}

input標簽沒有關閉標簽。

我的問題代碼如下

的HTML

<form action="tambahMatkul.php" method="post" id="ambil_matkul">  
    <table class="tabel table-bordered table-stripped table-responsive">
        <tr>
            <th width="5%">cek</th>
            <th width="19%">Kode</th>
            <th width="19%">Mata Kuliah</th>
            <th width="19%">W/P</th>
            <th width="19%">SKS</th>
            <th width="19%">Kelas</th>
        </tr>
        <?php include "koneksi.php";
        $query = $connect->query("SELECT * FROM matakuliah")or die(mysqli_error($connect));
        while($mahasiswa = $query->fetch_array()){
        ?>
        <tr>
            <td><input type="checkbox" name="cek[]" value="<?php echo $mahasiswa['kodeMatkul'] ?>" /></td>
            <td><?php echo $mahasiswa['kodeMatkul']; ?></td>
            <td><?php echo $mahasiswa['namaMatkul']; ?></td>
            <td><?php echo $mahasiswa['pilihan']; ?></td>
            <td><?php echo $mahasiswa['sks']; ?></td>
            <td><?php echo $mahasiswa['kelas']; ?></td>
        </tr>
        <?php } ?>
    </table>
    <hr/>
    <div class="btn-group"><button class="btn btn-sm btn-success" type="submit"><i class="fa fa-plus"></i> Tambah</button></div>
</form>

的PHP

<?php 
    $value = '';
    foreach($_POST['cek'] as $code) $value .= '(NULL, "161402133", "{$code}"),';// this loop add each value of $_POST['cek'] into $value as format reacquired for insert. 
    //echo $value;
    $connect = mysqli_connect("localhost","root","","krs");
    $query = mysqli_query($connect, 'INSERT INTO krs VALUES'.rtrim($value,',').';'); //rtrim($value,',') removes the last occurrence of comma (,)
    if($query)echo "Matakuliah sudah ditambahkan.";
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM