Pages
28 Februari 2011
Materi -10 : FILE ADMIN: TAMBAH, EDIT, dan HAPUS DATA
Pada akhir tutorial ini, kita akan mencoba mengggabungkan materi tutorial yang lalu (1 s/d 4) menjadi sebauah satu kesatuan file seperti gambar disamping. Untuk itu dibutuhkan beberapa file, antara lain :
1. koneksi.php
2. admin.php (file utama)
3. admin_tambah.php
4. hapus_user.php
5. edit_user.php
6. update_user.php
7. dan, create database (disini nama database : data_tkj2, tabel admin: id,user,nama,password,email)
untuk lebih jelasnya, perhatikan script-script berikut :
KONEKSI.PHP
[?php
$host="localhost";
$userdb="root";
$passdb="";
$namadb="data_tkj2";
$sambung=mysql_connect($host,$userdb,$passdb);
$konek=mysql_select_db($namadb,$sambung);
if(!$konek)
{echo "koneksi ke server gagal....";
}
?>
ADMIN.PHP
include ("1_koneksi.php");
$seleksi=mysql_select_db($namadb);
if(!$seleksi)
{ die("database tidak diaktifkan/tidak ada");
}
echo "[h2]Tambah, Hapus, Edit User[/h2]
[h4]Selamat Datang Administrator[/h4]";
mysql_query("DELETE FROM admin WHERE id=$baris[0]");
$hasil = mysql_query("SELECT * FROM admin order by id asc limit 50 ");
echo "[table border=1]\n";
echo "[tr bgcolor=#D9D9CC align='center']
[td]Id[/td]
[td]Username[/td]
[td]Password[/td]
[td]Sandi[/td]
[td]Email[/td]
[/tr]\n";
$i=1;
while ($baris = mysql_fetch_row($hasil)) {
$bgWr = $i % 2 == 1?"bgcolor=#D9D9FF":"";
echo "[tr $bgWr ]
[td]$baris[0][/td]
[td] $baris[1][/td]
[td]$baris[2][/td]
[td]$baris[3][/td]
[td]$baris[4][/td]
[td][a href=11_edit_user.php?id=$baris[0]]Edit[/a] |
[a href=10_hapus_user.php?id=$baris[0]]Hapus[/a] |
[a href=9_admin_tambah.php]Tambah[/a][/td]
[/tr]\n";
$i++;
}
echo "[/table]\n";
?>
ADMIN_TAMBAH.PHP
[form method=POST action='']
[table]
[tr][td]Username[/td] [td] : [input type=text name=user][/td][/tr]
[tr][td]Password[/td] [td] : [input type=password name=password][/td][/tr]
[tr][td]Nama Lengkap[/td] [td] : [input type=text name=nama size=30][/td][/tr]
[tr][td]E-mail[/td] [td] : [input type=text name=email size=30][/td][/tr]
[tr][td colspan=2][input type=submit value=Simpan][/td][/tr]
[/table]
[/form]
[?php
include ("1_koneksi.php");
$seleksi=mysql_select_db($namadb);
if(!$seleksi)
{ die("database tidak diaktifkan/tidak ada");
}
if($user=="" || $password=="") {
echo "Catatan :Username dan Password tak boleh kosong...atau...";
echo"[a href='5_form.php']ulangi[/a]\n";
}
else {
$pilih=mysql_query("select * from admin ");
$isi=mysql_query("INSERT INTO admin SET
user='$user',nama='$nama',pass=md5('$password'),email='$email'");
echo" Data telah ditambah.....[br][br]";
}
echo "[h2]Tambah User[/h2]
[h4]Selamat Datang Administrator[/h4]";
mysql_query("DELETE FROM admin WHERE id=$baris[0]");
$hasil = mysql_query("SELECT * FROM admin order by id asc limit 50 ");
echo "[table border=1]\n";
echo "[tr bgcolor=#D9D9CC align='center']
[td]Id[/td]
[td]Username[/td]
[td]Password[/td]
[td]Sandi[/td]
[td]Email[/td]
[/tr]\n";
$i=1;
while ($baris = mysql_fetch_row($hasil)) {
$bgWr = $i % 2 == 1?"bgcolor=#D9D9FF":"";
echo "[tr $bgWr ]
[td]$baris[0][/td]
[td] $baris[1][/td]
[td]$baris[2][/td]
[td]$baris[3][/td]
[td]$baris[4][/td]
[td][a href=11_edit_user.php?id=$baris[0]]Edit[/a] |
[a href=10_hapus_user.php?id=$baris[0]]Hapus[/a] |
[a href=9_admin_tambah.php]Tambah[/a][/td]
[/tr]\n";
$i++;
}
echo "[/table]\n";
?]
[a href='8_admin.php']Refresh....[/a]
EDIT_USER.PHP
[?php
include "1_koneksi.php";
$edit = mysql_query("SELECT * FROM admin WHERE id='$_GET[id]'");
$r = mysql_fetch_array($edit);
echo "[h2]Edit User[/h2]
[form method=POST action=12_update_user.php]
[input type=hidden name=id value='$r[id]']
[table]
[tr][td]Username[/td] [td] : [input type=text name=id_user value='$r[user]'][/td][/tr]
[tr][td]Password[/td] [td] : [input type=text name=pass] *) [/td][/tr]
[tr][td]Nama Lengkap[/td] [td] : [input type=text name=nama size=30 value='$r[nama]'][/td][/tr]
[tr][td]E-mail[/td] [td] : [input type=text name=email size=30 value='$r[email]'][/td][/tr]
[tr][td colspan=2]*) Apabila password tidak diubah, dikosongkan saja.[/td][/tr]
[tr][td colspan=2][input type=submit value=Update]
[input type=button value=Batal onclick=self.history.back()][/td][/tr]
[/table][/form]";
?]
UPDATE_USER.PHP
[?php
include ("1_koneksi.php");
// Apabila password tidak diubah
if (empty($_POST[password])) {
mysql_query("UPDATE admin SET id = '$_POST[id]',
nama = '$_POST[nama]',
email = '$_POST[email]'
WHERE id = '$_POST[id]'");
}
// Apabila password diubah
else{
$pass=md5($_POST[password]);
mysql_query("UPDATE admin SET id = '$_POST[id]',
password = '$pass',
nama = '$_POST[nama]',
email = '$_POST[email]'
WHERE id = '$_POST[id]'");
}
header('location:8_admin.php');
?]
HAPUS_USER.PHP
[?php
include "1_koneksi.php";
mysql_query("DELETE FROM admin WHERE id='$_GET[id]'");
header('location:8_admin.php');
?]
Catatan: seperti biasa, tinggal di Copy - Paste di Notepad,
kemudian tinggal Replace tanda "[]" dengaN "<>"