1

I have confusion in inserting multiple records in one row using single checkbox with php mysql

This is my form table

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

This is my form action

<?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.";

i don't know what to do? still beginner . please help me master

ino
  • 2,345
  • 1
  • 15
  • 27

3 Answers3

0

you need to loop and array to get multiple checkbox values. Below are the example

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

also i am going to reference This POST for more details

Post Author name:sean-walsh

Some more thing are wrong in your code:

1: Remove Database connection in loop

I recommend this code to insert these values into Database

$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);
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • Two problems: 1) Unless they check all the boxes, there won't be enough values in `$a`, since only checked boxes are in `$_POST['cek']`. 2) You have an extra `,` at the end of the values. – Barmar Dec 29 '17 at 07:19
  • @Barmar thanks your response. but when form submit only `checked` check box value pass.. please read this article https://www.formget.com/php-checkbox/ – Bilal Ahmed Dec 29 '17 at 07:22
  • @Barmar. you are right `,` extra at the end of string. – Bilal Ahmed Dec 29 '17 at 07:23
  • @Barmar thanks for correction. i have update my answer – Bilal Ahmed Dec 29 '17 at 07:25
  • I know only the checked boxes are passed. That's the problem. – Barmar Dec 29 '17 at 07:26
  • Did you forget to put quotes around `$checkData`? – Barmar Dec 29 '17 at 07:26
  • @Barmar i have update my answer, i know some time without concatenation errors occur. but in this case without concatenation it's work. check this http://sandbox.onlinephpfunctions.com/code/fbaf0feefa722a96b69e2abfda905e5ef4e03974 – Bilal Ahmed Dec 29 '17 at 07:33
  • It's still the same problem. You will have different numbers of values depending on how many boxes are checked. But the query has to insert the same number of values as there are table columns. – Barmar Dec 29 '17 at 07:35
  • We really need an answer to the question I posted in the comments above before we can give a proper answer. – Barmar Dec 29 '17 at 07:37
0

You need to process the POST values and them save in one query.

For example:

$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.";
}
neattom
  • 364
  • 2
  • 7
  • This can't be right. The number of `$_POST['cek']` values will depend on the number of boxes that are checked, but your query requires there to be values for every column in the table. – Barmar Dec 29 '17 at 07:16
  • `$_POST['cek']` contains only checked checkboxes. – neattom Dec 29 '17 at 07:20
  • That's the problem I was pointing out. `$values` will have a different number of values depending on which checkboxes are checked, but the query requires there to be enough values to fill in every column of the table. – Barmar Dec 29 '17 at 07:21
  • Never mind, I misread your code. You're creating multiple rows, not multiple columns in the same row. – Barmar Dec 29 '17 at 07:24
  • But the question says "multiple records in one row". He probably needs to normalize his schema for this to be the right answer. I suspect he's actually using a single column with a comma-separated list. – Barmar Dec 29 '17 at 07:25
  • We really need an answer to the question I posted in the comments above before we can give a proper answer. – Barmar Dec 29 '17 at 07:37
  • i thought i could insert all these one row values :") not just the checked one bsc of while function... – Sinta Anjelina Dec 29 '17 at 12:40
0

input tag doesn't have close tag.

My code for your problem is below

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.";
?>
GYaN
  • 2,327
  • 4
  • 19
  • 39