-3

I got a problem in my code I try to insert my data to database phpmyadmin,when I click submit button, and i got notices an Undefined Index...this is my code :

This is for PHP:

    <?php
require_once("koneksi.php");

    $NIP_KARYAWAN = "";
    $NAMA_KARYAWAN = "";
    $LEVEL = "";
    $USERNAME = "";
    $PASSWORD = "";

    if(isset($_POST["simpan"]))
    {
        $NIP_KARYAWAN = $_POST["NIP_KARYAWAN"];
        $NAMA_KARYAWAN = $_POST["NAMA_KARYAWAN"];
        $LEVEL = $_POST["LEVEL"];
        $USERNAME = $_POST["USERNAME"];
        $PASSWORD = $_POST["PASSWORD"];

            $query = "insert into karyawan (NIP_KARYAWAN,NAMA_KARYAWAN,LEVEL,USERNAME,PASSWORD) values ('$NIP_KARYAWAN','$NAMA_KARYAWAN','$LEVEL','$USERNAME','$PASSWORD')";
            mysql_query($query);

            ?><script>alert('Data berhasil ditambah');</script><?php
            ?><script>document.location.href='karyawan.php';</script><?php
            die(0);
    }
?>

and then this is my HTML :

<div class="row">
                <div class="col-lg-6">
                    <form role="form" action="" method="post">
                        <div class="form-group">
                            <label for="NIP_KARYAWAN">NIP</label>
                            <input type="number" class="form-control" placeholder="Masukkan NIP" id="NIP_KARYAWAN" name="NIP_KARYAWAN" value="<?php echo $NIP_KARYAWAN; ?>">
                        </div>

                        <div class="form-group">
                            <label for="NAMA_KARYAWAN">Nama</label>
                            <input type="text" class="form-control" placeholder="Masukkan Nama" id="NAMA_KARYAWAN" name="NAMA_KARYAWAN" value="<?php echo $NAMA_KARYAWAN; ?>">
                        </div>

                        <div class="form-group">
                            <label for="LEVEL">Level</label>
                            <select class="form-control" type="text" id="LEVEL" name="LEVEL" value="<?php echo $LEVEL; ?>">
                                <option>Manajerial</option>
                                <option>Operasional</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="col-lg-6">
                    <form role="form" action="" method="post">
                        <div class="form-group">
                            <label for="USERNAME">Username</label>
                            <input type="number" class="form-control" placeholder="Masukkan Username" id="USERNAME" name="USERNAME" value="<?php echo $USERNAME; ?>">
                        </div>

                        <div class="form-group">
                            <label for="PASSWORD">Password</label>
                            <input type="password" class="form-control" placeholder="Masukkan Password" id="PASSWORD" name="PASSWORD" value="<?php echo $PASSWORD; ?>">
                        </div>
                    </form>
                </div>
                <div class="col-lg-12" align="center">
                <form role="form" action="" method="post">
                    <button id="demo-1" type="submit" name="simpan" class="btn btn-primary"><i class="fa fa-save fa-lg"></i> Simpan</button>&nbsp&nbsp&nbsp
                    <button id="demo-2" type="submit" name="ubah" class="btn btn-warning"><i class="fa fa-refresh fa-lg"></i> Ubah</button>&nbsp&nbsp&nbsp
                    <button id="demo-3" type="submit" name="hapus" class="btn btn-danger"><i class="fa fa-dropbox fa-lg"></i> Hapus</button>
                </form>
                </div>                    
            </div>

This are the notices :

Notice: Undefined index: NIP_KARYAWAN in C:\xampp\htdocs\pencatatan\karyawan.php on line 13

Notice: Undefined index: NAMA_KARYAWAN in C:\xampp\htdocs\pencatatan\karyawan.php on line 14

Notice: Undefined index: LEVEL in C:\xampp\htdocs\pencatatan\karyawan.php on line 15

Notice: Undefined index: USERNAME in C:\xampp\htdocs\pencatatan\karyawan.php on line 16

Notice: Undefined index: PASSWORD in C:\xampp\htdocs\pencatatan\karyawan.php on line 17

Caps Lock
  • 3
  • 4
  • Please paste the error which notified you of an `Undefined Index` into your question. – skrilled Apr 12 '16 at 19:32
  • i've edited my question @skrilled – Caps Lock Apr 12 '16 at 19:34
  • 6
    You have multiple forms on that page. Only one will actually post. – Patrick Q Apr 12 '16 at 19:35
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – David Apr 12 '16 at 19:35
  • 1
    The form with the submit button(s) doesn't have any of the values you're looking for. The form(s) with [some of] the values you're looking for don't have submit buttons. – David Apr 12 '16 at 19:36
  • sorry, i did not know that @PatrickQ – Caps Lock Apr 12 '16 at 19:47
  • thank you for your help and explanations :) @David – Caps Lock Apr 12 '16 at 19:47
  • Welcome to Stack Overflow! The `mysql_*` functions in PHP are deprecated and shouldn't be used. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines Apr 16 '16 at 09:34

1 Answers1

0

As @PatrickQ stated, you have multiple forms. The submit buttons are in their own form separate from the form with your expected values. You need to wrap your entire form (and all its respective input's and such) in a single <form>...</form>. Buttons and all.

<form role="form" action="" method="post">
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group">
                <label for="NIP_KARYAWAN">NIP</label>
                <input type="number" class="form-control" placeholder="Masukkan NIP" id="NIP_KARYAWAN" name="NIP_KARYAWAN" value="<?php echo $NIP_KARYAWAN; ?>">
            </div>

            <div class="form-group">
                <label for="NAMA_KARYAWAN">Nama</label>
                <input type="text" class="form-control" placeholder="Masukkan Nama" id="NAMA_KARYAWAN" name="NAMA_KARYAWAN" value="<?php echo $NAMA_KARYAWAN; ?>">
            </div>

            <div class="form-group">
                <label for="LEVEL">Level</label>
                <select class="form-control" type="text" id="LEVEL" name="LEVEL" value="<?php echo $LEVEL; ?>">
                    <option>Manajerial</option>
                    <option>Operasional</option>
                </select>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                <label for="USERNAME">Username</label>
                <input type="number" class="form-control" placeholder="Masukkan Username" id="USERNAME" name="USERNAME" value="<?php echo $USERNAME; ?>">
            </div>

            <div class="form-group">
                <label for="PASSWORD">Password</label>
                <input type="password" class="form-control" placeholder="Masukkan Password" id="PASSWORD" name="PASSWORD" value="<?php echo $PASSWORD; ?>">
            </div>
        </div>
        <div class="col-lg-12" align="center">
            <button id="demo-1" type="submit" name="simpan" class="btn btn-primary"><i class="fa fa-save fa-lg"></i> Simpan</button>&nbsp&nbsp&nbsp
            <button id="demo-2" type="submit" name="ubah" class="btn btn-warning"><i class="fa fa-refresh fa-lg"></i> Ubah</button>&nbsp&nbsp&nbsp
            <button id="demo-3" type="submit" name="hapus" class="btn btn-danger"><i class="fa fa-dropbox fa-lg"></i> Hapus</button>
        </div>                    
    </div>
</form>
Community
  • 1
  • 1
mferly
  • 1,646
  • 1
  • 13
  • 19