Online Course

SR02 - Tutorial Membuat Pengaturan Data Customer Di Halaman Admin

Membuat Pengelolaan Data Customer di Halaman Admin

Halaman pengelolaan customer / member menampilkan data customer yang dapat dikelola oleh admin, misal menambahkan, mengedit, menghapus.

Pertama, Buat tabel baru dengan nama tb_member di database anda

Struktur tabel tb_member adalah seperti ini

struktur-tb-member

Setelah anda membuat tabel member.

Silahkan anda download file berikut ini. File ini berisi 4 tabel yaitu tb_provinsi, tb_kabupaten, tb_kecamatan dan tb_kelurahan. Tabel ini sudah lengkap dengan data wilayah seluruh indonesia. Download dan IMPORT ke database anda. (Jika lupa cara import tabel silahkan buka kembali materi cara export import database)

DOWNLOAD WILAYAH.SQL

Hasil setelah anda mengIMPORT file ini maka akan tampil 4 tabel baru seperti berikut

tabel-provinsi

Tabel yang anda IMPORT saya beri warna kuning

Kedua, Buat file baru beri nama member.php simpan di folder ADMINWEB

Isi dari file member.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Data Pembeli
                            <a href="input_member.php" title="Input data"><button name="input" class="btn btn-success pull-right"><i class="fa fa-plus fa-fw"></i> Tambah Customer</button></a>

                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                Home
                            </li>
                            <li class="active">
                                Data Pembeli
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">

                    <!-- /.tabel responsive -->
                                <div class="table-responsive">
                                    <table class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Nama </th>
                                                <th>Gender </th>
                                                <th>No Telp</th>
                                                <th>Email</th>
                                                <th>Alamat</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $pg = isset( $_GET['pg'] ) ? $_GET['pg'] : "";
                                        $batas = 10; /*batas tampilan setiap halaman*/
                                        if ( empty( $pg ) ) {
                                        $posisi = 0;
                                        $pg = 1;
                                        } else {
                                        $posisi = ( $pg - 1 ) * $batas;
                                        }
                                        /*Jika variabel $pg kosong maka akan menampilkan halaman pertama dengan batas baris 10*/

                                        $ambildata=mysqli_query($conect, "SELECT * FROM tb_member, tb_kelurahan, tb_kecamatan, tb_kabupaten, tb_provinsi where tb_member.id_kelurahan=tb_kelurahan.id_kelurahan and tb_kelurahan.id_kecamatan=tb_kecamatan.id_kecamatan and tb_kecamatan.id_kabupaten=tb_kabupaten.id_kabupaten and tb_kabupaten.id_provinsi=tb_provinsi.id_provinsi order by id_member desc limit $posisi, $batas");
                                        $jumlah=mysqli_num_rows($ambildata);  /*mysql_num_rows untuk menghitung total baris di tabel databse*/
                                        if($jumlah == 0){  //jika tidak ada data
                                            ?>
                                        <tr>
                                            <td colspan="8">Tidak Terdapat Data</td>
                                        </tr>
                                        <?php
                                        }else{
                                        //jika ada data di tb_brand
                                        while($a=mysqli_fetch_array($ambildata)){ /*mysql_fetch array untuk mengambil data di setiap field di tabel databse*/
                                        ?>
                                        <tr>
                                            <td><?php echo $posisi=$posisi+1;?></td>
                                            <td><?php echo $a['nama_member'];?></td>
                                            <td><?php echo $a['jk_member'];?></td>
                                            <td><?php echo $a['telp_member'];?></td>
                                            <td><?php echo $a['email_member'];?></td>
                                            <td><?php echo $a['alamat_member'];?>, <?php echo $a['nama_kelurahan'];?>, <?php echo $a['nama_kecamatan'];?>, <?php echo $a['nama_kabupaten'];?>, <?php echo $a['nama_provinsi'];?>, <?php echo $a['pos_member'];?></td>
                                            <td><a href="hapus_member.php?id_member=<?php echo $a['id_member'];?>" onclick="return confirm('Yakin akan meghapus data ini')" title="Hapus data"><button class="btn btn-danger btn-sm"> Hapus </button></a>
                                            <a href="edit_member.php?id_member=<?php echo $a['id_member'];?>" title="Edit data"><button class="btn btn-info btn-sm"> Ubah </button> </a>
                                            </td>
                                        </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                                <!-- /.tabel responsive -->

                                <div class="text-center">
                                    <?php
                                //script paging, untuk menampikan setiap halaman
                                $jml_data = mysqli_num_rows(mysqli_query($conect, "SELECT * FROM tb_member, tb_kelurahan, tb_kecamatan, tb_kabupaten, tb_provinsi where tb_member.id_kelurahan=tb_kelurahan.id_kelurahan and tb_kelurahan.id_kecamatan=tb_kecamatan.id_kecamatan and tb_kecamatan.id_kabupaten=tb_kabupaten.id_kabupaten and tb_kabupaten.id_provinsi=tb_provinsi.id_provinsi order by id_member desc"));
                                $JmlHalaman = ceil($jml_data/$batas); //ceil digunakan untuk pembulatan keatas
                                if($jml_data != 0){
                                if ( $pg > 1 ) {
                                $link = $pg-1;
                                $prev = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Prev</button></a> ";
                                } else {
                                $prev = "<button name='prev' class='btn btn-default btn-sm'>Prev </button> ";
                                }
                                $nmr = '';
                                for ( $i = 1; $i<= $JmlHalaman; $i++ ){

                                if ( $i == $pg ) {
                                $nmr .= "<button name='prev' class='btn btn-primary btn-sm'>$i</button> ";
                                } else {
                                $nmr .= "<a href='?pg=$i'><button name='prev' class='btn btn-default btn-sm'>$i</button></a> ";
                                }
                                }
                                if ( $pg < $JmlHalaman ) {
                                $link = $pg + 1;
                                $next = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Next</button></a> ";
                                } else {
                                $next = "<button name='prev' class='btn btn-default btn-sm'>Next</button> ";
                                }
                                echo $prev.$nmr.$next;
                                ?>
                                <br><br>
                                <span class="text-muted">Menampilkan <?php echo $jumlah; ?> dari <?php echo $jml_data; ?> Record </span>
                                <?php
                                }
                                ?>
                                </div>

                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>

Hasil dari file member.php akan menampilkan seperti ini

daftar-member

Ketiga, Buat file baru beri nama input_member.php simpan di folder ADMINWEB

Isi dari file input_member.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Tambah Data Pembeli
                            <a href="member.php" title="Input data"><button name="input" class="btn btn-default pull-right">Back</button></a>

                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                Home
                            </li>
                            <li class="active">
                                Tambah Data baru
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <?php
                //variabel setiap input di form
                $nama=mysqli_real_escape_string($conect, $_POST['nama']);   //variabel nama
                $jk=mysqli_real_escape_string($conect, $_POST['jk']);   //variabel gender
                $alamat=mysqli_real_escape_string($conect, $_POST['alamat']);   //variabel alamat
                $telp=mysqli_real_escape_string($conect, $_POST['telp']);   //variabel telp
                $pos=mysqli_real_escape_string($conect, $_POST['pos']);   //variabel kodepos
                $provinsi=mysqli_real_escape_string($conect, $_POST['provinsi']);   //variabel prov
                $kabupaten=mysqli_real_escape_string($conect, $_POST['kabupaten']);   //variabel kabptn
                $kecamatan=mysqli_real_escape_string($conect, $_POST['kecamatan']);   //variabel kec
                $kelurahan=mysqli_real_escape_string($conect, $_POST['kelurahan']);   //variabel kelurahan
                $pass=mysqli_real_escape_string($conect, $_POST['pass']);   //variabel password
                $pass2=mysqli_real_escape_string($conect, $_POST['pass2']); //variabel ulangi password
                $email=mysqli_real_escape_string($conect, $_POST['email']);     //variabel email
                $validasi= strtolower(substr(strrchr($email,"@"),1));         //validasi email
                $pass3=md5($pass);    //password di enkripsi ke md5

                if(isset($_POST['simpan'])){
                    if(empty($nama)){  //jika nama kosong
                        $er_nama="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Lengkap !</div>";
                    }
                    elseif(empty($jk)){  //jika gender kosong
                        $er_jk="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Pilih Gender Anda !</div>";
                    }
                    elseif(empty($alamat)){  //jika alamat kosong
                        $er_alamat="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan alamat !</div>";
                    }
                    elseif(empty($provinsi)){  //jika prov kosong
                        $er_provinsi="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Provinsi !</div>";
                    }
                    elseif(empty($kabupaten)){  //jika kabupaten kosong
                        $er_kabupaten="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kabupaten !</div>";
                    }
                    elseif(empty($kecamatan)){  //jika Kecamatan kosong
                        $er_kecamatan="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kecamatan !</div>";
                    }
                    elseif(empty($kelurahan)){  //jika Kelurahan kosong
                        $er_kelurahan="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kelurahan !</div>";
                    }
                    elseif(empty($telp)){  //jika telepon kosong
                        $er_telp="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nomor Telepon !</div>";
                    }
                    elseif(!is_numeric($telp)){  //jika telepon bukan angka
                        $er_telp="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nomor Telepon Dengan Angka!</div>";
                    }
                    elseif(empty($email)){     //jika email kosong
                        $er_email="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Email Admin !</div>";
                    }
                    //jika email tidak bukan berakhiran @yahoo, gmail, atau bsi
                    elseif($validasi !="yahoo.com" && $validasi !="yahoo.co.id" && $validasi !="gmail.com" && $validasi !="bsi.ac.id"){
                        $er_email="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Email Dengan Format yahoo atau gmail !</div>";
                        }
                    elseif(empty($pass)){  //jika password kosong
                        $er_pass="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Password !</div>";
                    }
                    elseif(strlen ($pass)< 8){   //jika password kurang dari 8 karakter
                        $er_pass="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Password  Min. 8 Karakter!</div>";
                    }
                    elseif(strlen ($pass)> 15){  //jika password lebih dari 15 karakter
                        $er_pass="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Password  Max. 15 Karakter!</div>";
                        }
                    elseif(empty($pass2)){  //jika ulangi password kosong
                        $er_pass2="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Ulangi Password !</div>";
                    }
                    elseif($pass != $pass2){     //jika password dan ulangi password salah
                        $er_pass2="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Ulangi Password Dengan Benar !</div>";
                        }

                    else{   //jika semua field terisis, maka cek email apakah sudah terdaftar atau belum di tb_admin
                        $cek_email=mysqli_query($conect, "SELECT * FROM tb_member where email_member='$email'");
                        $email_ada=mysqli_num_rows($cek_email);
                        if($email_ada > 0){   //jika email sudah terdaftar
                            $er_email="<div class='alert alert-warning alert-dismissable'>
                            <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                            <i class='fa fa-info-circle'></i> Email Sudah Terdaftar !</div>";
                        }else{
                            //jika email belum terdaftar maka menyimpan data ke tb_admin
                            $save=mysqli_query($conect, "INSERT INTO tb_member (id_member,nama_member,email_member,pass_member,jk_member,alamat_member,id_kelurahan,telp_member,pos_member)values('','$nama','$email','$pass3','$jk','$alamat','$kelurahan','$telp','$pos')") or die(mysql_error());
                            if($save){
                                echo "<script>alert('Data Member Baru Berhasil DItambahkan');document.location='member.php'</script>";
                            }else{
                                $error="<div class='alert alert-danger alert-dismissable'>
                                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                                <i class='fa fa-info-circle'></i> Gagal Menyimpan !</div>";
                            }
                        }
                    }
                }
                //jika memlih L maka variabel $yes akan di checked
                if($_POST['jk']=="L"){
                $yes="checked";
                }
                //jika memlih P maka variabel $no akan di checked
                elseif($_POST['jk']=="P"){
                $no="checked";
                }
                ?>

                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">
                            <form action="" method="post" enctype="multipart/form-data" role="form">
                            <?php echo $error;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nama Lengkap</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Nama Member" name="nama" value="<?php echo $_POST['nama'];?>" maxlength="40">
                                </div>
                            </div>
                            <?php echo $er_nama;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">L/P</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <?php echo "<input type='radio' name='jk' class='form-check-input' value='L' $yes>Laki-Laki

                                <input type='radio' name='jk' class='form-check-input' value='P' $no>Perempuan  ";?>
                                </div>
                                </div>
                            <?php echo $er_jk;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Alamat</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <textarea class="form-control" placeholder="Alamat" name="alamat" rows="3"><?php echo $_POST['alamat'];?></textarea>
                                </div>
                            </div>
                            <?php echo $er_alamat;?>
                           <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Provinsi</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="provinsi" id="provinsi">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $cek=mysqli_query($conect, "SELECT * FROM tb_provinsi order by nama_provinsi");
                                        while($ye=mysqli_fetch_array($cek)){
                                        if($ye['id_provinsi'] == $_POST['provinsi']){
                                        ?>
                                        <option value="<?php echo $ye['id_provinsi'];?>" selected>
                                        <?php echo $ye['nama_provinsi'];?>
                                        </option>
                                        <?php
                                        }else{
                                        ?>
                                        <option value="<?php echo $ye['id_provinsi'];?>">
                                        <?php echo $ye['nama_provinsi'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                           </div>
                           <?php echo $er_provinsi;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kabupaten</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kabupaten" id="kabupaten">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kab=mysqli_query($conect, "SELECT * FROM tb_kabupaten order by nama_kabupaten");
                                        while($kb=mysqli_fetch_array($kab)){
                                        if($kb['id_kabupaten'] == $_POST['kabupaten']){
                                        ?>
                                        <option value="<?php echo $kb['id_kabupaten'];?>" selected>
                                        <?php echo $kb['nama_kabupaten'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kabupaten;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kecamatan</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kecamatan" id="kecamatan">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kec=mysqli_query($conect, "SELECT * FROM tb_kecamatan order by nama_kecamatan");
                                        while($kc=mysqli_fetch_array($kec)){
                                        if($kc['id_kecamatan'] == $_POST['kecamatan']){
                                        ?>
                                        <option value="<?php echo $kc['id_kecamatan'];?>" selected>
                                        <?php echo $kc['nama_kecamatan'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kecamatan;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kelurahan</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kelurahan" id="kelurahan">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kel=mysqli_query($conect, "SELECT * FROM tb_kelurahan order by nama_kelurahan");
                                        while($kl=mysqli_fetch_array($kel)){
                                        if($kl['id_kelurahan'] == $_POST['kelurahan']){
                                        ?>
                                        <option value="<?php echo $kl['id_kelurahan'];?>" selected>
                                        <?php echo $kl['nama_kelurahan'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kelurahan;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kode Pos</label>
                                <div class="col-lg-2 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Kode Pos" name="pos" value="<?php echo $_POST['pos'];?>" maxlength="10">
                                </div>
                            </div>
                            <?php echo $er_pos;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nomor Telpon</label>
                                <div class="col-lg-5 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Nomor Telpon" name="telp" value="<?php echo $_POST['telp'];?>" maxlength="40">
                                </div>
                            </div>
                            <?php echo $er_telp;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Email</label>
                                <div class="col-lg-6 col-md-7 col-sm-7 col-xs-12">
                                <input type="email" class="form-control" placeholder="Email" name="email" value="<?php echo $_POST['email'];?>" maxlength="100">
                                </div>
                            </div>
                            <?php echo $er_email;?>

                            <hr>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Password</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input  type="password" class="form-control" placeholder="Password" name="pass" value="<?php echo $_POST['pass'];?>" maxlength="15">
                                </div>
                            </div>
                            <?php echo $er_pass;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Ulangi Password</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input  type="password" class="form-control" placeholder="Ulangi Password" name="pass2" value="<?php echo $_POST['pass2'];?>" maxlength="15">
                                </div>
                            </div>
                            <?php echo $er_pass2;?>


                            <button type="submit" name="simpan" class="btn btn-success">Simpan</button>

                            </form>
                                <!-- /.form -->
                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>
<script type="text/javascript">
$(document).ready(function () {

    $(document).on('change', '#provinsi', function () {
        var p = this.value;
        $.ajax({
            url: "kabupaten.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kabupaten').html(data);
            }
        })
    });

    $(document).on('change', '#kabupaten', function () {
        var p = this.value;
        $.ajax({
            url: "kecamatan.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kecamatan').html(data);

            }
        })
    });

    $(document).on('change', '#kecamatan', function () {
        var p = this.value;
        $.ajax({
            url: "kelurahan.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kelurahan').html(data);

            }
        })
    });
});

</script>

Buat file dengan nama kabupaten.php dengan isi file seperti ini

<?php
error_reporting(0);
include "../assets/relasi/koneksi.php";
$output = '';

$query = "SELECT * FROM tb_kabupaten WHERE id_provinsi = '".$_POST["p"]."' order by nama_kabupaten";
$result = mysqli_query($conect, $query);

$output .= '<option value="">-- Pilih --</option>';

while($ye=mysqli_fetch_array($result)){

$output .= '<option value="' .$ye['id_kabupaten']. '">' .$ye['nama_kabupaten']. '</option>';

}

echo $output;
?>

Buat file dengan nama kecamatan.php dengan isi file seperti ini

<?php
error_reporting(0);
include "../assets/relasi/koneksi.php";
$output = '';

$query = "SELECT * FROM tb_kecamatan WHERE id_kabupaten = '".$_POST["p"]."'";
$result = mysqli_query($conect, $query);

$output .= '<option value="">-- Pilih --</option>';

while($ye=mysqli_fetch_array($result)){

$output .= '<option value="' .$ye['id_kecamatan']. '">' .$ye['nama_kecamatan']. '</option>';

}

echo $output;
?>

Buat file dengan nama kelurahan.php dengan isi file seperti ini

<?php
error_reporting(0);
include "../assets/relasi/koneksi.php";
$output = '';

$query = "SELECT * FROM tb_kelurahan WHERE id_kecamatan = '".$_POST["p"]."'";
$result = mysqli_query($conect, $query);

$output .= '<option value="">-- Pilih --</option>';

while($ye=mysqli_fetch_array($result)){

$output .= '<option value="' .$ye['id_kelurahan']. '">' .$ye['nama_kelurahan']. '</option>';

}

echo $output;
?>

Hasil dari file input_member.php akan menampilkan seperti ini

input-member

Keempat, Buat file baru beri nama edit_member.php simpan di folder ADMINWEB

Isi dari file edit_member.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Ubah Data Pembeli
                            <a href="member.php" title="Input data"><button name="input" class="btn btn-default pull-right">Back</button></a>

                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                               Home
                            </li>
                            <li class="active">
                                Edit Data Pembeli
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <?php
                //ambil data dari tb memebr, tb provinsi, tb-kabupaten, tb_kecamatan, tb_kelurahan
                $tampildata=mysqli_query($conect, "SELECT * FROM tb_member, tb_kelurahan, tb_kecamatan, tb_kabupaten, tb_provinsi where tb_member.id_member='$_GET[id_member]' and tb_member.id_kelurahan=tb_kelurahan.id_kelurahan and tb_kelurahan.id_kecamatan=tb_kecamatan.id_kecamatan and tb_kecamatan.id_kabupaten=tb_kabupaten.id_kabupaten and tb_kabupaten.id_provinsi=tb_provinsi.id_provinsi");
                $b=mysqli_fetch_array($tampildata);
                $idp = $b['id_provinsi']; //variabel dari id_provinsi
                $idkb = $b['id_kabupaten'];  //variabel dari id_kabupaten
                $idkc = $b['id_kecamatan'];  //variabel dari id_kecamatan
                $idkl = $b['id_kelurahan'];  //variabel dari id_kelurahan

                //variabel setiap input di form
                $nama=mysqli_real_escape_string($conect, $_POST['nama']);   //variabel nama
                $jk=mysqli_real_escape_string($conect, $_POST['jk']);   //variabel gender
                $alamat=mysqli_real_escape_string($conect, $_POST['alamat']);   //variabel alamat
                $telp=mysqli_real_escape_string($conect, $_POST['telp']);   //variabel telp
                $pos=mysqli_real_escape_string($conect, $_POST['pos']);   //variabel kodepos
                $provinsi=mysqli_real_escape_string($conect, $_POST['provinsi']);   //variabel prov
                $kabupaten=mysqli_real_escape_string($conect, $_POST['kabupaten']);   //variabel kabptn
                $kecamatan=mysqli_real_escape_string($conect, $_POST['kecamatan']);   //variabel kec
                $kelurahan=mysqli_real_escape_string($conect, $_POST['kelurahan']);   //variabel kelurahan
                $email=mysqli_real_escape_string($conect, $_POST['email']);     //variabel email
                $validasi= strtolower(substr(strrchr($email,"@"),1));         //validasi email

                if(isset($_POST['simpan'])){
                    if(empty($nama)){  //jika nama kosong
                        $er_nama="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Lengkap !</div>";
                    }
                    elseif(empty($jk)){  //jika gender kosong
                        $er_jk="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Pilih Gender Anda !</div>";
                    }
                    elseif(empty($alamat)){  //jika alamat kosong
                        $er_alamat="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan alamat !</div>";
                    }
                    elseif(empty($provinsi)){  //jika prov kosong
                        $er_provinsi="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Provinsi !</div>";
                    }
                    elseif(empty($kabupaten)){  //jika kabupaten kosong
                        $er_kabupaten="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kabupaten !</div>";
                    }
                    elseif(empty($kecamatan)){  //jika Kecamatan kosong
                        $er_kecamatan="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kecamatan !</div>";
                    }
                    elseif(empty($kelurahan)){  //jika Kelurahan kosong
                        $er_kelurahan="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nama Kelurahan !</div>";
                    }
                    elseif(empty($telp)){  //jika telepon kosong
                        $er_telp="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nomor Telepon !</div>";
                    }
                    elseif(!is_numeric($telp)){  //jika telepon bukan angka
                        $er_telp="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Nomor Telepon Dengan Angka!</div>";
                    }
                    elseif(empty($email)){     //jika email kosong
                        $er_email="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Email Admin !</div>";
                    }
                    elseif($validasi !="yahoo.com" && $validasi !="yahoo.co.id" && $validasi !="gmail.com" && $validasi !="bsi.ac.id"){
                        $er_email="<div class='alert alert-warning alert-dismissable'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <i class='fa fa-info-circle'></i> Masukan Email Dengan Format yahoo atau gmail !</div>";
                        }
                    else{   //jika semua field terisis, maka cek email apakah sudah terdaftar atau belum di tb_admin
                    if($email != $b['email_member']){ //jika email yang dimasukan berbeda dengan email sebelumnya
                        $cek_email=mysqli_query($conect, "SELECT * FROM tb_member where email_member='$email'");
                        $email_ada=mysqli_num_rows($cek_email);
                        if($email_ada > 0){   //jika email sudah terdaftar
                            $er_email="<div class='alert alert-warning alert-dismissable'>
                            <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                            <i class='fa fa-info-circle'></i> Email Sudah Terdaftar !</div>";
                        }else{
                            //jika email belum terdaftar maka menyimpan data ke tb_admin
                            $save=mysqli_query($conect, "UPDATE tb_member set nama_member='$nama',email_member='$email',jk_member='$jk',alamat_member='$alamat',id_kelurahan='$kelurahan',telp_member='$telp',pos_member='$pos' where id_member='$_GET[id_member]'");
                            if($save){
                                echo "<script>alert('Data Member Berhasil Diubah');document.location='member.php'</script>";
                            }else{
                                $error="<div class='alert alert-danger alert-dismissable'>
                                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                                <i class='fa fa-info-circle'></i> Gagal Menyimpan !</div>";
                            }
                        }
                    }else{
                            //jika email tidak diubah
                            $save=mysqli_query($conect, "UPDATE tb_member set nama_member='$nama',jk_member='$jk',alamat_member='$alamat',id_kelurahan='$kelurahan',telp_member='$telp',pos_member='$pos' where id_member='$_GET[id_member]'");
                            if($save){
                                echo "<script>alert('Data Member Berhasil diubah');document.location='member.php'</script>";
                            }else{
                                $error="<div class='alert alert-danger alert-dismissable'>
                                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                                <i class='fa fa-info-circle'></i> Gagal Menyimpan !</div>";
                            }
                        }
                    }
                }
                //jika memlih L maka variabel $yes akan di checked
                if($b['jk_member']=="L"){
                $yes="checked";
                }
                //jika memlih P maka variabel $no akan di checked
                elseif($b['jk_member']=="P"){
                $no="checked";
                }
                ?>

                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">

                    <form action="" method="post" enctype="multipart/form-data" role="form">
                            <?php echo $error;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nama Lengkap</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Nama Member" name="nama" value="<?php echo $b['nama_member'];?>" maxlength="40">
                                </div>
                            </div>
                            <?php echo $er_nama;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">L/P</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <?php echo "<input type='radio' name='jk' class='form-check-input' value='L' $yes>Laki-Laki

                                <input type='radio' name='jk' class='form-check-input' value='P' $no>Perempuan  ";?>
                                </div>
                                </div>
                            <?php echo $er_jk;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Alamat</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <textarea class="form-control" placeholder="Alamat" name="alamat" rows="3"><?php echo $b['alamat_member'];?></textarea>
                                </div>
                            </div>
                            <?php echo $er_alamat;?>
                           <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Provinsi</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="provinsi" id="provinsi">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $cek=mysqli_query($conect, "SELECT * FROM tb_provinsi order by nama_provinsi");
                                        while($ye=mysqli_fetch_array($cek)){
                                        if($ye['id_provinsi'] == $idp){
                                        ?>
                                        <option value="<?php echo $ye['id_provinsi'];?>" selected>
                                        <?php echo $ye['nama_provinsi'];?>
                                        </option>
                                        <?php
                                        }else{
                                        ?>
                                        <option value="<?php echo $ye['id_provinsi'];?>">
                                        <?php echo $ye['nama_provinsi'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                           </div>
                           <?php echo $er_provinsi;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kabupaten</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kabupaten" id="kabupaten">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kab=mysqli_query($conect, "SELECT * FROM tb_kabupaten where id_provinsi='$idp' order by nama_kabupaten");
                                        while($kb=mysqli_fetch_array($kab)){
                                        if($kb['id_kabupaten'] == $idkb){
                                        ?>
                                        <option value="<?php echo $kb['id_kabupaten'];?>" selected>
                                        <?php echo $kb['nama_kabupaten'];?>
                                        </option>
                                        <?php
                                        }else{
                                        ?>
                                        <option value="<?php echo $kb['id_kabupaten'];?>">
                                        <?php echo $kb['nama_kabupaten'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kabupaten;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kecamatan</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kecamatan" id="kecamatan">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kec=mysqli_query($conect, "SELECT * FROM tb_kecamatan where id_kabupaten='$idkb' order by nama_kecamatan");
                                        while($kc=mysqli_fetch_array($kec)){
                                        if($kc['id_kecamatan'] == $idkc){
                                        ?>
                                        <option value="<?php echo $kc['id_kecamatan'];?>" selected>
                                        <?php echo $kc['nama_kecamatan'];?>
                                        </option>
                                        <?php
                                        }else{
                                        ?>
                                        <option value="<?php echo $kc['id_kecamatan'];?>">
                                        <?php echo $kc['nama_kecamatan'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kecamatan;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kelurahan</label>
                                <div class="col-lg-3 col-md-7 col-sm-7 col-xs-12">
                                <select class="form-control" name="kelurahan" id="kelurahan">
                                        <option value="">-- Pilih --</option>
                                        <?php
                                        $kel=mysqli_query($conect, "SELECT * FROM tb_kelurahan where id_kecamatan='$idkc' order by nama_kelurahan");
                                        while($kl=mysqli_fetch_array($kel)){
                                        if($kl['id_kelurahan'] == $idkl){
                                        ?>
                                        <option value="<?php echo $kl['id_kelurahan'];?>" selected>
                                        <?php echo $kl['nama_kelurahan'];?>
                                        </option>
                                        <?php
                                        }else{
                                        ?>
                                        <option value="<?php echo $kl['id_kelurahan'];?>">
                                        <?php echo $kl['nama_kelurahan'];?>
                                        </option>
                                        <?php
                                        }
                                        }
                                        ?>
                                </select>
                                </div>
                            </div>
                            <?php echo $er_kelurahan;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kode Pos</label>
                                <div class="col-lg-2 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Kode Pos" name="pos" value="<?php echo $b['pos_member'];?>" maxlength="10">
                                </div>
                            </div>
                            <?php echo $er_pos;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nomor Telpon</label>
                                <div class="col-lg-5 col-md-7 col-sm-7 col-xs-12">
                                <input  type="text" class="form-control" placeholder="Nomor Telpon" name="telp" value="<?php echo $b['telp_member'];?>" maxlength="40">
                                </div>
                            </div>
                            <?php echo $er_telp;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Email</label>
                                <div class="col-lg-6 col-md-7 col-sm-7 col-xs-12">
                                <input type="email" class="form-control" placeholder="Email" name="email" value="<?php echo $b['email_member'];?>" maxlength="100">
                                </div>
                            </div>
                            <?php echo $er_email;?>
                            <button type="submit" name="simpan" class="btn btn-success">Simpan</button>

                            </form>
                                <!-- /.form -->
                            </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>
<script type="text/javascript">
$(document).ready(function () {

    $(document).on('change', '#provinsi', function () {
        var p = this.value;
        $.ajax({
            url: "kabupaten.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kabupaten').html(data);
            }
        })
    });

    $(document).on('change', '#kabupaten', function () {
        var p = this.value;
        $.ajax({
            url: "kecamatan.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kecamatan').html(data);

            }
        })
    });

    $(document).on('change', '#kecamatan', function () {
        var p = this.value;
        $.ajax({
            url: "kelurahan.php",
            method: "POST",
            data: { p: p },
            success: function (data) {
                $('#kelurahan').html(data);

            }
        })
    });
});

</script>

Hasil dari file edit_member.php akan menampilkan seperti ini

edit-member

Kelima, Buat file baru beri nama hapus_member.php simpan di folder ADMINWEB

Isi dari file hapus_member.php seperti ini

<?php
session_start();
//panggil koneksi
include "../assets/relasi/koneksi.php";

$id=$_GET['id_member'];  //dapatkan id

if(isset($id)){ //jika ada id
//hapus data di tabel
$hapus=mysqli_query($conect, "DELETE from tb_member where id_member='$id'");
//jika hapus berhasil
if($hapus){
    echo"<script>alert('Data Berhasil di Hapus');document.location='member.php'</script>";
}
else{ //jika gagal
    echo"<script>alert('Error Bos !!');document.location='member.php'</script>";
}

}
?>

Oke, Selesai

 

 

Develindo Web | Tutorial Pemrograman Website