Multiple insert native php 7 mysqli

saya punya 2 buah file utk melakukan query insert multiple data. namun saat disimpan menampilkan undefined index : nis, (cek pada tabel di database hanya field nis yang tidak terisi). Barangkali ada yang sudah pernah ngalamin ? berikut kode dari filenya

file add.php

<form action="save.php" method="POST">
              <div class="table-responsive">
              <table id="" class="table table-bordered table-striped">
                <thead>
                <tr class="text-center">
                  <th>No</th>
                  <th>NIS</th>
                  <th>Nama Siswa</th>
                  <th>Kode Materi</th>
                  <th>Nilai</th>
                  <th>Semester</th>
                  <th>Tahun Akademik</th>
                </tr>
                </thead>

                <tbody>
                  <?php
                  $no= 1;
                  $sql = mysqli_query($db,"SELECT * FROM tb_siswa WHERE angkatan='$angkatan' AND kelas = '$kelas' ORDER BY tgldaftar asc");
                  while ($nilai = mysqli_fetch_array($sql)) {
                    $nis = $nilai['nis'];
                    $nama = $nilai['nama'];

                    echo "
                <tr class='text-center'>
                 <td>$no</td>
                 <td><input type='text' name='nis[]' value='$nis' size='9' disabled></td>
                 <td><input type='text' value='$nama' disabled></td>
                 <td><input type='text' name='kdmateri[]' size='5'></td>
                 <td><input type='text' name='huruf[]' size='1'></td>
                 <td><input type='text' name='semester[]' value='1' size='2'/></td>
                 <td><input type='text' name='thakademik[]' value='2020/2021' size='9' /></td>
                </tr>

                    ";
                    $no++;
                  }
                 ?>
                             </tbody>

              </table>
                <button type="submit" name="proses" class="btn btn-dark btn-sm">Proses Nilai</button>
              </div>
              </form>

file save.php

<?php
require 'conn.php';
$data = $_POST;

echo "<pre>";
var_dump($data);

if (isset($_POST['proses'])) {
	$_POST['nis'];
$count = count( (array) $_POST['nis']);
}

for ($i=0; $i <=$count ; $i++) {
	$sql = "INSERT INTO `tb_nilai` (`nis`, `kdmateri`, `huruf`, `semester`, `thakademik`) VALUES ('{$_POST['nis'][$i]}', '{$_POST['kdmateri'][$i]}', '{$_POST['huruf'][$i]}', '{$_POST['semester'][$i]}', '{$_POST['thakademik'][$i]}')";

	$db->query($sql);
}

avatar wukufdilvan
@wukufdilvan

2 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Coba Ini bro

<pre> &lt;?php require 'conn.php';

if (isset($_POST['proses'])) {

$nis = $_POST[&#039;nis&#039;];
$kdmateri = $_POST[&#039;kdmateri&#039;];
$huruf =  $_POST[&#039;huruf&#039;];
$semester =  $_POST[&#039;semester&#039;];
$thakademik = $_POST[&#039;thakademik&#039;];

$count = count($nis);

for ($i=0; $i &amp;lt;=$count ; $i++)
 { 
    $sql = &quot;INSERT INTO `tb_nilai` (`nis`, `kdmateri`, `huruf`,`semester`, `thakademik`) VALUES (&#039;{$nis[$i]}&#039;,&#039;{$kdmateri[$i]}&#039;, &#039;{$huruf[$i]}&#039;,&#039;{$semester[$i]}&#039;, &#039;{$thakademik[$i]}&#039;)&quot;;
    $db-&amp;gt;query($sql);

}

} </pre>

avatar abubakar
@abubakar

72 Kontribusi 19 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban