我的表中有7多个列,称为“ pembelajaran(课程)”,我将第一列设置为用于连接到另一个表的标识符。
我想通过使用从其他表中通过php代码获取的选择和选项将数据插入7列。
我已经尝试使用$ _POST方法多次插入数据,当我提交按钮时,var_dump仅显示第一列数据。我知道我是新来的。希望你们中的一个可以帮助我解决我的问题。
这是我的代码
<div class="form-group">
<?php
$krID = $krk['kurikulum'];
$mpl = $db->prepare("SELECT * FROM mapel WHERE parent = '$krID'");
$mpl->execute();
while($mp = $mpl->fetch(PDO::FETCH_ASSOC)){?>
<div class="row">
<div class="col-sm-6 col-md-6">
<div class="form-group">
<label for="<?=$mp['kode']?>"><?php echo $mp['mapel'];?></label>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="form-group">
<select name="<?=$mp['id']?>" class="form-control" required>
<option value="0">-Pilih Guru-</option>
<?php
$gr = $db->prepare("SELECT * FROM guru WHERE parent = 0 ORDER BY id");
$gr->execute();
while($mg = $gr->fetch(PDO::FETCH_ASSOC)){?>
<option value="<?=$mg['id']?>"><?=$mg['nama']?>, <?=$mg['jabatan']?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<?php } ?>
</div>
这是我的php代码,用于执行“提交”按钮:
<div class="form-group">
<input type="submit" name="save" value="simpan" class="btn btn-success">
<?php
if(isset($_POST['save'])){
$datamapel = "pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk";
$guru = $datamapel;
$simpanpembelajaran = $db->prepare("INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('$_POST[kelas]','$guru)");
$simpanpembelajaran->execute();
var_dump($simpanpembelajaran);
}
?>
</div>
这是var_dump();说
Notice: Undefined variable: guru in D:\xampp\htdocs\raport\index\kurikulum.php on line 275
Notice: Undefined variable: guru in D:\xampp\htdocs\raport\index\kurikulum.php on line 275
object(PDOStatement)#7 (1) { ["queryString"]=> string(131) "INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('4','','')" }
我仍然试图找到最佳结果。这是我最后的尝试。我尝试使用爆炸来识别任何名称。
<div class="form-group">
<input type="submit" name="save" value="simpan" class="btn btn-success">
<?php
if(isset($_POST['save'])){
$datamapel = "pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk";
$guru = explode(",",$datamapel);
$simpanpembelajaran = $db->prepare("INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('$_POST[kelas]','$guru')");
$simpanpembelajaran->execute();
var_dump($simpanpembelajaran);
}
?>
</div>
但是结果仍然会出现这样的错误。
Notice: Array to string conversion in D:\xampp\htdocs\raport\index\kurikulum.php on line 275
object(PDOStatement)#7 (1) { ["queryString"]=> string(133) "INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('4','Array')" }
如果有任何建议更改我的方法以仅将值正确存储到我的数据库中,请显示它。
更新
我使用此代码工作,但是即使我从仍然存储到数据库的select 0中选择一个选项,这也是我最后的代码。 我的选择代码
<select name="kode[<?=$mp['kode']?>]" class="form-control" required>
<option value="0">-Pilih Guru-</option>
<?php
$gr = $db->prepare("SELECT * FROM guru WHERE parent = 0 ORDER BY id");
$gr->execute();
while($mg = $gr->fetch(PDO::FETCH_ASSOC)){?>
<option value="<?=$mg['id']?>"><?=$mg['nama']?>, <?=$mg['jabatan']?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<?php } ?>
</div>
这是我的“提交”按钮代码。
<div class="form-group">
<input type="submit" name="save" value="simpan" class="btn btn-success">
<?php
if(isset($_POST['save'])){
$kelas = $_POST['kelas'];
$guru = implode(", ",$_POST['kode']);
$checkheula = $db->prepare("SELECT * FROM pembelajaran WHERE kelas = '$kelas'");
$checkheula->execute();
$check = $checkheula->rowcount();
if($check > 0){
$datanya = $checkheula->fetch(PDO::FETCH_ASSOC);
$ids = $datanya['id'];
$kelas = $_POST['kelas'];
$guru = implode(", ",$_POST['kode']);
$timpah = $db->prepare("UPDATE pembelajaran SET kelas = '$kelas', mapel = 0, pkn = '$guru[0]', pai = '$guru[1]', ind = '$guru[2]', mtk = '$guru[3]', ipa = '$guru[4]', ips = '$guru[5]', ing = '$guru[6]', sbk = '$guru[7]', pjok = '$guru[8]', pra = '$guru[9]', tik = '$guru[10]', btq = '$guru[11]', bp = '$guru[12]', sun = '$guru[13]', mlk = '$guru[14]' WHERE id = '$ids'");
$timpah->execute();
var_dump($timpah);
}else{
$guru = implode(", ",$_POST['kode']);
$simpanpembelajaran = $db->prepare("INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('$_POST[kelas]','','$guru[0]','$guru[1]','$guru[2]','$guru[3]','$guru[4]','$guru[5]','$guru[6]','$guru[7]','$guru[8]','$guru[9]','$guru[10]','$guru[11]','$guru[12]','$guru[13]','$guru[14]')");
$simpanpembelajaran->execute();
var_dump($simpanpembelajaran);
}
}
?>
</div>
然后var_dump在下面显示类似这样的信息
object(PDOStatement)#7 (1) { ["queryString"]=> string(226) "UPDATE pembelajaran SET kelas = '4', mapel = 0, pkn = '9', pai = ',', ind = ' ', mtk = '1', ipa = '1', ips = ',', ing = ' ', sbk = '2', pjok = '1', pra = ',', tik = ' ', btq = '6', bp = ',', sun = ' ', mlk = '1' WHERE id = '1'" }
这是我的pembelajaran
桌上的图片。
Pembelajaran Table
再次更新
当我想检索mapel
之后和parent
列之前的每个列的值时,发现了一个新错误。也许,您需要一张照片我遇到什么错误。以下链接为您展示了我的作品。
UI from my work
请专注于
Notice: Array to string conversion in D:\xampp\htdocs\raport\index\kurikulum.php on line 379 Array
在列guru
上,我想通过使用插入的值来获取teacher's name (guru)
,我试图使用数组,但是不起作用。
这是我的代码
<div class="col-sm-6 col-md-6">
<hr><h1 class="text-muted text-center">Info Pembelajaran</h1><hr>
<table class="table table-hover">
<thead>
<tr>
<td>Mapel</td>
<td>Kelas</td>
<td>Guru</td>
<td>Opsi</td>
</tr>
<?php
include '../controller/set-kurikulum.php';
$trk = $db->prepare("SELECT * FROM pembelajaran WHERE parent = 0");
$trk->execute();
while($p = $trk->fetch(PDO::FETCH_ASSOC)){
$dk = $p['kelas'];
$klsss = $db->prepare("SELECT * FROM kelas WHERE id = '$dk'");
$klsss->execute();
while ($mn = $klsss->fetch(PDO::FETCH_ASSOC)) {
?>
<tr class="bg-info">
<td colspan="4"><b><?=$mn['kelas']?></b></td>
</tr>
<?php } ?>
</thead>
<?php $dk = $p['kelas'];
$klsss = $db->prepare("SELECT * FROM kelas WHERE id = '$dk'");
$klsss->execute();
while ($mn = $klsss->fetch(PDO::FETCH_ASSOC)) {
$k = $krk['kurikulum'];
$mpls = $db->prepare("SELECT * FROM mapel WHERE parent = '$k'");
$mpls->execute();
while($mps = $mpls->fetch(PDO::FETCH_ASSOC)){
$dks = $mn['id'];
$ida = $db->prepare("SELECT pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk FROM pembelajaran WHERE kelas = '$dks'");
$ida->execute();
// $jjm = $db->prepare("SELECT * FROM guru WHERE id = '$ida'");
while($guru = $ida->fetch(PDO::FETCH_ASSOC)){
// $std = implode(", ", $guru);
?>
<tbody>
<tr>
<td><?=$mps['mapel']?></td>
<td><?=$mn['kelas']?></td>
<td>
<?php
$namaguru = array(
0 => 'Tidak Terdaftar',
1 => 'Nurul Hidayat, S.Pd.',
2 => 'Madsana, Am.Pd.',
3 => 'Mahdian Umarella, S.Pd.',
4 => 'Khaerul Munajat, SH.I',
5 => 'Humaedi, S.SoS.i',
6 => 'Siti Nurjanah, S.Pd.',
7 => 'Awaludin, S.Pd.',
8 => 'Suhayati, S.Pd.',
9 => 'Supriyanto, S.Pd.',
10 => 'Karta, S.Pd.',
11 => 'Sutihat, S.Pd.',
12 => 'Ahmad Royandi, S.Pd.',
13 => 'Hendra, S.Pd.',
14 => 'Anwar Husadat, S.Pd.',
15 => 'Ripah Dahlia, S.Pd.',
16 => 'Eka, S.Pd.',
17 => 'Friska, S.Pd.',
18 => 'Rini, S.Pd.',
19 => 'Rudi, S.Pd.',
20 => 'Munajah, S.Pd.',
21 => 'Muhamad Aceng Nuryani, S.Pd.',
22 => 'Julian, S.Pd.',
23 => 'Ahmad Suwondo, S.Pd.',
24 => 'Fajar Anwar, S.Pd.',
25 => 'Hasan',
);
// var_dump(implode("<br> ",$namaguru));
$gd = array_column($namaguru, 'pkn');
// echo $gd;
if($guru = $namaguru){echo $namaguru;}
?>
</td>
<td>
<a href="" class="btn btn-xs btn-warning"><span class="glyphicon glyphicon-pencil"></span></a>
<a href="" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove"></span></a>
<a href=""></a>
</td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<tr>
</tr>
</tbody>
如果有其他方法可以建议我,我会尽力而为,非常感谢您的帮助。
答案 0 :(得分:0)
在此代码段中,
$datamapel = "pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk";
$guru = explode(",",$datamapel);
$simpanpembelajaran = $db->prepare("INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk)VALUES('$_POST[kelas]','$guru')");
变量$guru
是一个数组,因此$datamapel
的值存储在$guru
中,如下所示,
$guru[0] -> pkn
$guru[1] -> pai
...
以此类推
在SQL查询中,您尝试将存储在$guru
中的数据插入不同的列。我想你想要的是这个,
$simpanpembelajaran = $db->prepare("INSERT INTO pembelajaran (kelas, mapel, pkn, pai, ind, mtk, ipa, ips, ing, sbk, pjok, pra, tik, btq, bp, sun, mlk) VALUES ('$_POST[kelas]','$guru[0]','$guru[1]', ...)");
...
,这不是代码的一部分,您需要添加其余的索引。还要注意,查询中提到的列数必须等于VALUES
中的字段数。