无法将数据插入数据库MySQL

时间:2016-04-12 19:31:55

标签: php mysql

我的代码中出现问题我尝试将数据插入数据库phpmyadmin,当我点击提交按钮时,我注意到了一个未定义的索引...这是我的代码:

这适用于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);
    }
?>

然后这是我的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>

这是通知:

  

注意:未定义索引:NIP_KARYAWAN in   第13行的C:\ xampp \ htdocs \ pencatatan \ karyawan.php

     

注意:未定义的索引:NAMA_KARYAWAN in   第14行的C:\ xampp \ htdocs \ pencatatan \ karyawan.php

     

注意:未定义的索引:LEVEL in   第15行的C:\ xampp \ htdocs \ pencatatan \ karyawan.php

     

注意:未定义的索引:USERNAME in   第16行的C:\ xampp \ htdocs \ pencatatan \ karyawan.php

     

注意:未定义的索引:PASSWORD in   第17行的C:\ xampp \ htdocs \ pencatatan \ karyawan.php

1 个答案:

答案 0 :(得分:0)

@PatrickQ所述,您有多种形式。 提交按钮采用自己的形式,与具有预期值的表单分开。您需要将整个表单(及其所有相应的input等)包装在一个<form>...</form>中。按钮和所有。

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