0

I 'm making an input in the form of tables.

for example like this :

No | Time | Day (automatic generate) |

No automatic generate | Time Automatic Generate | Activity input form |

sample php code generate table.

<form enctype="multipart/form-data" action="input_jadual.php" method="post">
<table id="example" class="table table-striped table-bordered" width="100%" cellspacing="0">
            <thead>
            <tr>
                <th width="10">No</th>
                <th width="60"><center>Waktu</center></th>
<?php
$begin = new DateTime($ttl1);
$end = new DateTime($ttl2);
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dayList = array(
    'Sunday' => 'Minggu',
    'Monday' => 'Senin',
    'Tuesday' => 'Selasa',
    'Wednesday' => 'Rabu',
    'Thursday' => 'Kamis',
    'Friday' => 'Jumat',
    'Saturday' => 'Sabtu'
);
foreach($daterange as $date){
    $tgl=$date->format("d-m-Y");
    $hariss = date('l', strtotime($tgl));
    $haris=$dayList[$hariss];
    if ($haris=='Minggu'){
        $warna='red';
        $dis='disabled';
     }else{
        $warna='';
        $dis='';
     }
    ?>
<th><center><input type="hidden" name="hari[]" value="<?php echo $haris; ?>"/><input type="hidden" name="tgl[]" value="<?php echo $tgl; ?>"/><font color="<?php echo $warna; ?>"><?php echo $haris; ?><br><?php echo $tgl; ?></font></center></th>
<?php  } ?>
            </tr>
            </thead>
            <tbody>
<?php $nos=1; $arrlength = count($waktus); for($x = 0; $x < $arrlength; $x++) { ?>
<?php if ($waktus[$x]=='12.00-13.00'){
                        ?>
                        <tr disabled>
                            <td class="data" align="center"><?php echo $nos;?></td>
                            <td class="data" align="center"><input type="hidden" name="waktu[]" value="<?php echo $waktus[$x];?>"/><?php echo $waktus[$x];?></td>
                            <?php $no=1; for($i = 0; $i < (is_numeric($fields)?$fields:1); $i++) { ?>
                            <td class="data" align="center">
                            <i>Istirahat, Sholat & Makan</i>
                            <?php } ?> 
                        </tr>
                        <?php } elseif ($waktus[$x]=='10.15-10.30'){
                        ?>
                        <tr disabled>
                            <td class="data" align="center"><?php echo $nos;?></td>
                            <td class="data" align="center"><input type="hidden" name="waktu[]" value="<?php echo $waktus[$x];?>"/><?php echo $waktus[$x];?></td>
                            <?php $no=1; for($i = 0; $i < (is_numeric($fields)?$fields:1); $i++) { ?>
                            <td class="data" align="center">
                            <i>Coffee Break</i>
                            <?php } ?> 
                        </tr>
                        <?php } elseif ($waktus[$x]=='15.15-15.45'){
                        ?>
                        <tr disabled>
                            <td class="data" align="center"><?php echo $nos;?></td>
                            <td class="data" align="center"><input type="hidden" name="waktu[]" value="<?php echo $waktus[$x];?>"/><?php echo $waktus[$x];?></td>
                            <?php $no=1; for($i = 0; $i < (is_numeric($fields)?$fields:1); $i++) { ?>
                            <td class="data" align="center">
                            <i>Coffee Break</i>
                            <?php } ?> 
                        </tr>
                        <?php } elseif ($waktus[$x]=='18.00-19.00'){
                        ?>
                        <tr disabled>
                            <td class="data" align="center"><?php echo $nos;?></td>
                            <td class="data" align="center"><input type="hidden" name="waktu[]" value="<?php echo $waktus[$x];?>"/><?php echo $waktus[$x];?></td>
                            <?php $no=1; for($i = 0; $i < (is_numeric($fields)?$fields:1); $i++) { ?>
                            <td class="data" align="center">
                            <i>Istirahat, Sholat & Makan</i>
                            <?php } ?> 
                        </tr>
<?php
     }else{
?>
<tr>
<td class="data" align="center"><?php echo $nos;?></td>
                <td class="data" align="center"><input type="hidden" name="waktu[]" value="<?php echo $waktus[$x];?>"/><?php echo $waktus[$x];?></td>
                <?php $no=1; for($i = 0; $i < (is_numeric($fields)?$fields:1); $i++) { ?>
                <td class="data" align="center">
        <input list="modul" name="modul[]" style="border:none" placeholder="Pilih Modul">
          <datalist id="modul">
          <?php include 'include/conn.php';
                $nama_pelatihan=$_POST['nama_pelatihan'];
                $tampil1=mysql_db_query(@$db,"select * from modul where nama_pelatihan='$nama_pelatihan'",@$koneksi);   
                while ($row=mysql_fetch_array($tampil1))
                    {
                    $nama_modul=$row['nama_modul'];
                    $jp=$row['jp'];
                  ?>
            <option value="<?php echo $nama_modul ?> (<?php echo $jp ?>JP)">
                    <?php } ?>
          </datalist>
        <input list="widyaiswara" name="widyaiswara[]" style="border:none" placeholder="Pilih Widyaiswara">
          <datalist id="widyaiswara">
          <?php include 'include/conn.php';
                $tampil2=mysql_db_query(@$db,"select * from data_pokok",@$koneksi); 
                while ($row=mysql_fetch_array($tampil2))
                    {
                    $nama=$row['nama'];
                  ?>
            <option value="<?php echo $nama ?>">
                    <?php } ?>
          </datalist>
                <?php } ?> 
</tr>
<?php
     }
    ?>

<?php $nos++; } ?>
<input type="hidden" name="code" value="<?php echo $code ?>"/>
<input type="hidden" name="bidang_diklat" value="<?php echo $bidang_diklat; ?>"/>
<input type="hidden" name="nama_pelatihan" value="<?php echo $nama_pelatihan ?>"/>
<input type="hidden" name="ttl1" value="<?php echo $ttl1 ?>"/>
<input type="hidden" name="ttl2" value="<?php echo $ttl2 ?>"/>
<input type="hidden" name="status" value="draft"/>
</tbody>
</table>
<input type="submit" name="simpan" title="Simpan" value="S I M P A N" class="btn btn-success btn-block btn-lg bayang" onclick="cekForm()">
</form>

php code insert :

<?php 
    include "include/conn.php";
    $tanggal;
    if (isset($_POST['simpan']))//periksa apakah user telah menekan submit, dengan menggunakan parameter setingan isi
    {
    $bidang_diklat=$_POST['bidang_diklat'];
    $nama_pelatihan=$_POST['nama_pelatihan'];
    $ttl1=$_POST['ttl1'];
    $ttl2=$_POST['ttl2'];
    $status=$_POST['status'];
    $code=$_POST['code'];

    $jam=$_POST['jam'];
    $hari=$_POST['hari'];
    $tgl=$_POST['tgl'];
    $modul=$_POST['modul'];
    $widyaiswara=$_POST['widyaiswara'];

    //periksa jika data yang dimasukan belum lengkap
        if (empty($bidang_diklat))
            {
                echo "<script> document.location.href='index.php?page=Jadual&status=draft&error=8'</script>";
            }
            $upload=mysql_db_query($db,"INSERT INTO jadual (bidang_diklat,nama_pelatihan,ttl1,ttl2,status,code) VALUES('$bidang_diklat','$nama_pelatihan','$ttl1','$ttl2','$status','$code')");
            $upload2=mysql_db_query($db,"INSERT INTO jadual_kegiatan (code,jam,hari,tgl,modul,widyaiswara) VALUES('$code','$jam','$hari','$tgl','$modul','$widyaiswara')");
            echo "<script> document.location.href='index.php?page=Jadual&status=draft'; </script>";
    }
    else
    {
        unset($_POST['simpan']);
    }
    ?>

But not insert in mysql . . . please help concept & sample code for insert.

Thanks guys in advance! :D

  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 07 '15 at 19:09
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 07 '15 at 19:09
  • you're also submitting `[]`-named fields, then trying to stuff the entire array directly into your query, meaning you'll be inserting the literal word `Array`, **NOT** the form data. plus your db code is simply assuming nothing could ever fail, it appears. never ever assume success when dealing with external resources. – Marc B Dec 07 '15 at 19:12
  • If the `(isset($_POST['simpan'])` condition is being met, then you should be getting an error if the insert isn't occurring. Make sure you've enabled php error reporting so you can see the error(s). – devlin carnate Dec 07 '15 at 19:12
  • If an error occurs, the data will remain stored hehehe . . . – Dammah SifLa Dec 07 '15 at 19:15
  • hmmmmm You have an array of input form ?? – Dammah SifLa Dec 07 '15 at 19:18

0 Answers0