2

I want to input the data into the table id_ss ss_kat and enter ss_kat id_kat on the table, so the data will be displayed on the input kat ss_kat the checkbox menu, but I have tried repeatedly 2 can not, there are approximately erornya where?

enter image description here

I want to submit the checkbox in table kat and sample index from image here

<?php
    include "../../inc/conn.php";
?>

<div class="row">
<div class="col-md-6">
    <form action="proses.php" method="POST" enctype="multipart/form-data" style="margin-top:20px;">
        <input type="hidden" name="id_ss_kat" />

        <?php
                $ss = mysql_query("SELECT * from ss");
        ?>

        <select class="form-control" name="id_ss">
        <?php
            while ($rows=mysql_fetch_array($ss)) 
            { ?>
            <option class="form-control" value="<?php echo $rows['id_ss'];?>" name="id_ss"><?php echo $rows['name'];?></option>

        <?php
            }
        ?>
        </select>
        <br>
        <?php
            $kat = mysql_query("SELECT * from kat WHERE kat_status='1'");
        ?>

        <?php
            while ($rows=mysql_fetch_array($kat)) 
            { ?>

        <input type="checkbox" name="id_kat[<?php echo $rows['id_kat'];?>]" value="<?php echo $rows['id_kat'];?>"><?php echo $rows['nama'];?>

        <?php
            }
        ?>

        <br>
        <input class="btn btn-primary" type="submit" name="ssf" value="SAVE">
        <br>
    </form> 
    <br>

</div>
</div>

and here is proses.php

<?php
    if (isset($_POST['ssf'])) {
        $id_ss_kat          = $_POST['id_ss_kat'];
        $id_kat             = $_POST['id_kat'];
        $id_ss              = $_POST['id_ss'];

        foreach($id_kat as $kat) {
            $source.=$kat.", ";
        }
        $one=substr($source,0,-1);

        $q      = "INSERT into ss_kat VALUES('$id_ss_kat','$one','$id_sekolah')";
        $dq     = mysql_query($q);

        if ($dq) {
            echo "berhasil";
        }else{
            mysql_error();
        }

    }
?>

The index look like in the picture below:

enter image description here

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
Anisa
  • 21
  • 1

1 Answers1

0

You have to select the corresponding columns you want to insert your data:

$q = "INSERT into ss_kat (column1, column2, column3)
      VALUES ('$id_ss_kat','$one','$id_sekolah')";

You are also prone to SQL injections. You either use *_real_escape_string() or prepared statement, but I would prefer the latter.

Warning also that you are also using a deprecated mysql_* extensions, starting with PHP 5.5.0 and will be removed in PHP v7.

Do you really want to put the selected checkboxes in a single row in your database? Because in a normal scheme, you would prefer to store them in each designated row.

Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49