Postingan lainnya
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);
}
0
1 Jawaban:
Jawaban Terpilih
Coba Ini bro
<pre> <?php require 'conn.php';
if (isset($_POST['proses'])) {
$nis = $_POST['nis'];
$kdmateri = $_POST['kdmateri'];
$huruf = $_POST['huruf'];
$semester = $_POST['semester'];
$thakademik = $_POST['thakademik'];
$count = count($nis);
for ($i=0; $i &lt;=$count ; $i++)
{
$sql = "INSERT INTO `tb_nilai` (`nis`, `kdmateri`, `huruf`,`semester`, `thakademik`) VALUES ('{$nis[$i]}','{$kdmateri[$i]}', '{$huruf[$i]}','{$semester[$i]}', '{$thakademik[$i]}')";
$db-&gt;query($sql);
}
} </pre>
0