0

I have more than 7 columns in my table called 'pembelajaran (lessons)' the first column i set as identifier for connecting to another table.

I want to insert data to 7 columns by using select and option fetched from other table with php code.

I've tried many time for insert data by using $_POST method, when I submit the button my var_dump just shows the first column data. I knew I am new of this. Hope one of you could help me with my problem.

This my code

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

This my php code to execute the submit button:

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

This the var_dump(); says

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','','')" }

I still try to find the best result. this my last trying. I try to use explode to identify any name.

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

but the result still get error like this.

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')" }

If any suggestion to change my method to get the value stored correctly to my database just, show it.

UPDATE

I use this code and work, but several column even I pick an option from select still 0 stored to database, this my last code. My select code

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

This my Submit button code.

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

And the var_dump shows information like this below

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'" }

This my pembelajaran table picture. Pembelajaran Table

UPDATE AGAIN I found a new error when I want to retrieve the value of every single columns after mapel and before parent column. Perhaps, you need a picture what error I faced. The link below shows you a picture of my work. UI from my work

please focus on Notice: Array to string conversion in D:\xampp\htdocs\raport\index\kurikulum.php on line 379 Array

on column guru I want to fetch teacher's name (guru) by using value inserted, I tried to use array but is not working.

this my code

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

if any another method just suggest me, I'll try to do it and I very appreciate for your help.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Can you show us the code in this kurikulum.php ? According to your vardump output there is an undefined variable called ```guru``` – Saurabh P Bhandari Jun 20 '19 at 03:40
  • Check if ```$guru[]``` is defined or not ? – Saurabh P Bhandari Jun 20 '19 at 03:43
  • @SaurabhPBhandari this code created at kurikulum.php I used to $_REQUEST to open another page by using menu – dhedheerik88 Jun 20 '19 at 03:48
  • I've already edited code above – dhedheerik88 Jun 20 '19 at 03:52
  • Regarding the update, what do you expect if not 0 ? Also any specific reason to use implode here ? Note that in your database the default value and data type of the column might effect your inserts and updates, for example, if you expect a empty character to be inserted in a column which supports only numeric data, then default value or 0 might be used – Saurabh P Bhandari Jun 21 '19 at 02:48
  • I expect the option of select `value="=$mg['id']?>"` stored to the database, so that why, I retrieve the id from `guru` and I set the default `0` only for `parent` column, The reason to use `implode` is I want to make an identifier for $_POST to recognize that I send to value of option with name of array `$datamapel`. sorry for my bad English. – dhedheerik88 Jun 21 '19 at 04:02
  • I request you to open another question, it seems to deviate from the original question asked "How to insert data to database using select with same name ?" – Saurabh P Bhandari Jun 21 '19 at 05:22

1 Answers1

0

In this code snippet,

$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')");

The variable $guru is an array, so the values of $datamapel is stored in $guru as follows,

$guru[0] -> pkn
$guru[1] -> pai
...

and so on

In your SQL query, you are trying to insert the data stored in $guru into different columns. I think what you want is this,

$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]', ...)");

..., this is not part of the code , you need to add the remaining indexes. Also note that the number of columns mentioned in the query needs to be equal to number of fields in VALUES.

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • I tried and success, but the following code insert an array of $datamapel not value of the option, how could it be? – dhedheerik88 Jun 20 '19 at 09:23
  • @dhedheerik88, which code are you talking about ? ```$datamapel``` is a string, you are converting that into an array of strings based on comma (delimiter) and storing it into ```$guru``` variable, so ```$guru``` is an array – Saurabh P Bhandari Jun 20 '19 at 11:01
  • yeah, it was a string converted to an array, when I submit var_dump shows me ('4','pai','pkn'....) Not a valur of select because I store the array to variable wothout $_POST. The next question should be could I store the array into like this $_POST['guru[]']? Or give me an advice. – dhedheerik88 Jun 21 '19 at 00:36
  • @dhedheerik88, if you want to send an array via POST request, you could just send a string like ```$datamapel``` and then explode it into an array, you can further edit your question – Saurabh P Bhandari Jun 21 '19 at 00:40
  • you can see the newest update of mine. would you like to check how I fill the 0 value? it happen even I select an option. – dhedheerik88 Jun 21 '19 at 01:32
  • @dhedheerik88, if this solved your question consider marking the answer as accepted – Saurabh P Bhandari Jun 21 '19 at 05:23