我在PHP中使用循环插入数据时遇到了问题。我想将两个或多个数据组成的“STATUS MATAKULIAH”和“KODE MATAKULIAH”插入到db_unifa.tdkrs表中。我不知道该怎么做。也许这里的任何人都可以帮助我解决我的问题。 这是我的代码:
<html>
//table head start
<table id="tableData" width='1028' border='1'>
<tr class="head">
<th width='32'><div align='center'><strong>STATUS MATAKULIAH</strong></div></th>
<th width='216'><div align='center'><strong>KODE MATAKULIAH</strong></div></th>
<th width='616'><div align='center'><strong>NAMA MATAKULIAH</strong></div></th>
<th width='60'><div align='center'><strong>SKS</strong></div></th>
<th width='30'><div align='center'><strong>PILIH</strong></div></th>
</tr>
//table head end
//retrieve data td from database start
<?php $sqlkk = mysql_query("SELECT DISTINCT stambuk FROM tnilaikkrekap WHERE stambuk=$_SESSION[stambuk]");
$i = 1;
while($hasilkk = mysql_fetch_array($sqlkk))
if($hasilkk > 0){
//mahasiswa korban konversi kurikulum
$sql_isi = mysql_query("SELECT tregmk.kmk,tmmatakuliah.nmk,ifnull(tmmatakuliah.sks,0) AS sks FROM tregmk JOIN tmmatakuliah
ON (tregmk.kmk=tmmatakuliah.kmk) LEFT JOIN v_all_nilai_kk ON ((tregmk.kmk=v_all_nilai_kk.kmk) AND
(v_all_nilai_kk.stambuk=$_SESSION[stambuk]) AND (v_all_nilai_kk.nilai > 2)) WHERE (tregmk.tahunajarn=$thajaran)
and (tregmk.semester=$smster) AND (tregmk.fakultas=$fakpilihan) AND (tregmk.prodi=$prodi) AND (tregmk.jenjang=$jenjang) AND
(v_all_nilai_kk.kmk is null) AND (tmmatakuliah.nokur=$no_kur)");
while($hasil_isi = mysql_fetch_array($sql_isi)){
$j = $hasil_isi[sks];
echo "<tr class='genap'>";
echo "<td align='center'><select name='statuskmk$i'><option name='baru$i' value='0'>Baru</option>
<option name='ulang' value='1'>Ulang</option><option name='perbaikan' value='3'>Perbaikan</option><option>Batal</option>
</select></td>";
echo "<td>$hasil_isi[kmk]</td>";
echo "<td>$hasil_isi[nmk]</td>";
echo "<td align='center'>$hasil_isi[sks]</td>";
echo "<td><div align='center'>";
echo "<input type='checkbox' id='id$i' name='statmk$i' value='$j'></input>";
echo "</div></td>";
echo "</tr>";
$i++;
}
} ?> </table>
//retrieve data end
//Insert data with while loop start
<?php
if($_POST['simpan']){
$tajaran = $_POST['thajaran'];
$semester = $_POST['sms'];
$fakultas = $_POST['fak'];
$propil = $_POST['pstudi'];
$jenpil = $_POST['jenjang'];
$ins_nid = $_POST['kdpa'];
$ins_iplalu = $_POST['ips'];
$ins_skscp = $_POST['csks'];
$ins_sksrc = $_POST['rsks'];
$ins_sksambil = $_POST['sksambil'];
$ins_ipk = $_POST['ipk'];
$ins_konsen = $_POST['konsent'];
$user = $_POST['nmmhs'];
$tanggal = $_POST['tanggal'];
$kdmk = $_POST['kdmk'];
$status = $_POST['myselect'];
include "konek.php";
for($i = 0; $i < count($kmk); $i++){
$insert = "INSERT INTO db_unifa.tdkrs (tdkrs.idkrs,tdkrs.kmk,tdkrs.statskmk,tdkrs.tanggal,tdkrs.user)
VALUES('$tajaran$semester$fakultas$propil$jenpil$_SESSION[stambuk]','$kdmk$i',left($status,1),$tanggal,'$user')";
$query = mysql_query($insert);
}
if ($query) {
echo "<script>alert('Data berhasil disimpan !')</script>";
//echo "<script>window.location.href = 'data.php?cabang=$cabang'</script>";
}
else {
echo "<script>alert('Error: Data gagal dimasukkan!')</script>";
}
}
?>
//Insert data with while loop end
请帮忙。
答案 0 :(得分:0)
<html>
<head>
<title>sample Page</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
var szTr;
szTr = szTr + "<tr class='genap'>";
szTr = szTr + "<td align='center'><select name='statuskmk$i'><option name='baru$i' value='0'>Baru</option>"
szTr = szTr + "<option name='ulang' value='1'>Ulang</option><option name='perbaikan' value='3'>Perbaikan</option><option>Batal</option>"
szTr = szTr + "/select></td>";
szTr = szTr + "<td>$hasil_isi[kmk]</td>";
szTr = szTr + "<td>$hasil_isi[nmk]</td>";
szTr = szTr + "<td align='center'>$hasil_isi[sks]</td>";
szTr = szTr + "<td><div align='center'>";
szTr = szTr + "<input type='checkbox' id='checkid' name='statmk' value='sample'></input>";
szTr = szTr + "</div></td>";
szTr = szTr + "</tr>";
$('#tableData').append(szTr)
$('#test').click(function () {
$('#tableData tr').find('td input[type="checkbox"]:checked').each(function () {
// alert($(this).attr('value'))
$('#result_value').val($(this).closest('tr').find('select').val())
$('#result_text').val($(this).closest('tr').find('select option:selected').text())
});
});
});
</script>
</head>
<body>
<input type="button" id="test" value="GetChecked Value"/><br />
selected combo value <input type="text" id="result_value"/><br />
selected combotext <input type="text" id="result_text"/>
<br />
<br />
<br />
<table id="tableData" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
</table>
</body>
</html>