使用php mysql的单个复选框在一行中插入多个记录

时间:2017-12-29 06:44:44

标签: php mysql checkbox

我使用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.";

我不知道该怎么办?还是初学者。请帮我掌握

3 个答案:

答案 0 :(得分:0)

您需要循环和数组以获取多个复选框值。以下是示例

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

我还将参考http://sedodream.com/2013/06/05/HowToPublishAVSWebProjectWithAPublishSettingsFile.aspx了解更多详情

作者姓名:This POST

您的代码中还有一些问题:

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);

答案 1 :(得分:0)

您需要处理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.";
}

答案 2 :(得分:0)

<?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."; ?> 标记没有关闭标记。

我的问题代码在下面

<强> HTML

import smtplib


content='Hello I am just checking email.'
mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('My email','Mypassword')
mail.send('From email','destiation password',content)
mail.close()

<强> PHP

        BasicConfigurator.Configure(nameRunRollFileAppender);
        log = LogManager.GetLogger(typeof(Tracer));
        log.Info("Output some data");