Kode: [Pilih]
create database universitas;
use universitas;
CREATE TABLE IF NOT EXISTS `mahasiswa` (
`nim` char(9) NOT NULL,
`nama` varchar(50) NOT NULL,
`tgl_lahir` date NOT NULL,
`alamat` varchar(200) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`nim`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
kalo udah bikin databasenya kita buat file koneksinya deh, kaya di bawah ini
Kode: [Pilih]
<?php
$host['nama_host'] = 'localhost';$user['nama_user'] = 'root';$pass['password_user'] = '';$db['nama_datbase'] = 'universitas';
$koneksi = mysql_connect ($host['nama_host'],$user['nama_user'],$pass['password_user']);
if( ! $koneksi)
{ echo 'Gagal koneksi jhon, coba diliat file koneksinya'; mysql_error();
}mysql_select_db($db['nama_datbase']) or die ("Database gak ada jhon, coba di cek dah".mysql_error());?>
simpan file di atas dengan nama konek.phpnah kalo udah sekarang kita bikin file TampilMahasiswa.php
Kode: [Pilih]
<?phpinclude_once "konek.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mahasiswa</title>
</head>
<body>
<table width="100%" align="center" border="1px">
<tr>
<td colspan="6"><strong><font size="+3"><p align="center"> DATA MAHASISWA </p></font></strong></td>
</tr>
<tr>
<td>NIM</td>
<td>NAMA</td>
<td>TANGGAL LAHIR</td>
<td>ALAMAT</td>
<TD>EMAIL</TD>
<TD>AKSI</TD>
</tr>
<?php
$sql = "select * from mahasiswa order by nim";$qry = mysql_query($sql,$koneksi) or die ("Query gagal");
while ($mahasiswa = mysql_fetch_array($qry))
{?>
<tr>
<td><?php echo $mahasiswa['nim']; ?></td>
<td><?php echo $mahasiswa['nama']; ?></td>
<td><?php echo $mahasiswa['tgl_lahir']; ?></td>
<td><?php echo $mahasiswa['alamat']; ?></td>
<td><?php echo $mahasiswa['email']; ?></td>
<td><a href="EditMahasiswa.php?kodeEdit=<?php echo $mahasiswa['nim']; ?>">Edit</a> | <a href="HapusMahasiswa.php?kodeHapus=<?php echo $mahasiswa['nim']; ?>">Hapus</a></td>
</tr>
<?php}?>
</table>
<a href="http://localhost/thread/">View List File</a>
</body>
</html>
Simpan file di atas dengan nama TampilMahasiswa.phplanjut kita bikin file tambahmahasiswa buat isi data mahasiswa yang mau dimasukin ke database
Kode: [Pilih]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tambah Mahasiswa</title>
</head>
<body>
<form method="post" name="FormSimpanMahasiswa" action="SimpanMahasiswa.php">
<table width="700px" border="0px" align="center">
<tr>
<td colspan="3"><strong><p align="center">Masukkan Data Mahasiswa</p></strong></td>
</tr>
<tr>
<td width="20%">NIM</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtNim" /></td>
</tr>
<tr>
<td width="20%">Nama</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtNama" /></td>
</tr>
<tr>
<td width="20%">Tanggal Lahir</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtTglLahir" /></td>
</tr>
<tr>
<td width="20%">Alamat</td>
<td width="5%"> : </td>
<td width="75%"><textarea name="TxtAlamat"></textarea></td>
</tr>
<tr>
<td width="20%">Email</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtEmail" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="SimpanMahasiswa" value="Simpen" /></td>
</tr>
</table>
</form>
</body>
</html>
simpan file di atas dengan nama TambahMahasiswa.phpkalo udah sekarang kita bikin file simpanmahasiswa.php buat nyimpen data yang udah diisiin di file tambah mahasiswa ke database
Kode: [Pilih]
<?phpif ($_POST['SimpanMahasiswa'])
{ $TxtNim = $_POST['TxtNim']; $TxtNama = $_POST['TxtNama']; $TxtTglLahir = $_POST['TxtTglLahir']; $TxtAlamat = $_POST['TxtAlamat']; $TxtEmail = $_POST['TxtEmail']; if (trim ($TxtNim == '')) { $pesan[] = 'Data NIM mahasiswa belum diisi '; } if (trim ($TxtNama == '')) { $pesan[] = 'Data nama mahasiswa belum diisi '; } if (trim ($TxtTglLahir == '')) { $pesan[] = 'Data tanggal lahir belum diisi'; } if (trim($TxtAlamat=='')) { $pesan[] = 'Data alamat belum diisi'; } if (trim ($TxtEmail=='')) { $pesan[] = 'data email belum diisi'; } if (! count ($pesan)==0) { $urutin_pesan =0; include_once 'TambahMahasiswa.php'; foreach ($pesan as $indeks => $pesan_error) { $urutin_pesan+=1; echo " <font color='red'>$urutin_pesan.$pesan_error<br /></font>"; } exit; } else { include_once 'konek.php'; $query = "insert into mahasiswa values ('$TxtNim','$TxtNama','$TxtTglLahir','$TxtAlamat','$TxtEmail')"; mysql_query ($query,$koneksi) or die ('gagal simpen data'.mysql_error()); echo "Proses simpan berhasil"; include_once 'TampilMahasiswa.php'; }
}
else
{ include 'TambahMahasiswa.php'; exit;
}?>
simpan file di atas dengan nama SimpanMahasiswa.php . oke lanjut bikin file edit mahasiswa
Kode: [Pilih]
<?phpif (! $_GET['kodeEdit']=="")
{ include_once "konek.php"; $sql = "select * from mahasiswa where nim = '".$_GET['kodeEdit']."'"; $qry = mysql_query($sql,$koneksi) or die ("gagal ngejalanin sql"); $mahasiswa = mysql_fetch_array ($qry); $TxtNim = $mahasiswa['nim']; $TxtNama = $mahasiswa['nama']; $TxtTglLahir = $mahasiswa['tgl_lahir']; $TxtAlamat = $mahasiswa['alamat']; $TxtEmail = $mahasiswa['email'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tambah Mahasiswa</title>
</head>
<body>
<form method="post" name="SimpanUbahMahasiswa" action="SimpanEditMahasiswa.php">
<table width="700px" border="0px" align="center">
<tr>
<td colspan="3"><strong><p align="center">Edit Data Mahasiswa</p></strong></td>
</tr>
<tr>
<td width="20%">NIM</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtNim" value="<?php echo $TxtNim; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td width="20%">Nama</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtNama" value="<?php echo $TxtNama; ?>" /></td>
</tr>
<tr>
<td width="20%">Tanggal Lahir</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtTglLahir" value="<?php echo $TxtTglLahir; ?>" /></td>
</tr>
<tr>
<td width="20%">Alamat</td>
<td width="5%"> : </td>
<td width="75%"><textarea name="TxtAlamat"> <?php echo $TxtAlamat; ?></textarea></td>
</tr>
<tr>
<td width="20%">Email</td>
<td width="5%"> : </td>
<td width="75%"><input type="text" name="TxtEmail" value="<?php echo $TxtEmail; ?>" /></td>
</tr>
<tr>
<td colspan="6"><input type="submit" name="SimpanEditMahasiswa" value="Simpen" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php}
else
{ include_once "TampilMahasiswa.php"; exit;
}
simpan file diatas dengan nama EditMahsiswa.phpfile simpan edit mahasiswa
Kode: [Pilih]
<?phpif ($_POST['SimpanEditMahasiswa'])
{ $TxtNim = $_POST['TxtNim']; $TxtNama = $_POST['TxtNama']; $TxtTglLahir = $_POST['TxtTglLahir']; $TxtAlamat = $_POST['TxtAlamat']; $TxtEmail = $_POST['TxtEmail']; if (trim ($TxtNim == '')) { $pesan[] = 'Data NIM mahasiswa belum diisi '; } if (trim ($TxtNama == '')) { $pesan[] = 'Data nama mahasiswa belum diisi '; } if (trim ($TxtTglLahir == '')) { $pesan[] = 'Data tanggal lahir belum diisi'; } if (trim($TxtAlamat=='')) { $pesan[] = 'Data alamat belum diisi'; } if (trim ($TxtEmail=='')) { $pesan[] = 'data email belum diisi'; } if (! count ($pesan)==0) { $urutin_pesan =0; include_once 'EditMahasiswa.php'; foreach ($pesan as $indeks => $pesan_error) { $urutin_pesan+=1; echo " <font color='red'>$urutin_pesan.$pesan_error<br /></font>"; } exit; } else { include_once 'konek.php'; $query = "update mahasiswa set nim='$TxtNim',nama='$TxtNama',tgl_lahir='$TxtTglLahir',alamat='$TxtAlamat',email='$TxtEmail' where nim='$TxtNim'"; mysql_query ($query,$koneksi) or die ('gagal simpen data'.mysql_error()); echo "Proses update berhasil"; include_once 'TampilMahasiswa.php'; }
}
else
{ include 'TampilMahasiswa.php'; exit;
}?>
simpan file diatas dengan nama SimpanEditMahasiswa .php
terakhir buat file hapus.php
Kode: [Pilih]
<?phpif(! $_GET['kodeHapus'] == "")
{ include_once "konek.php"; $sql="delete from mahasiswa where nim='".$_GET['kodeHapus']."'"; $qry = mysql_query($sql,$koneksi) or die ("Gagal hapus jhon"); echo "Data mahasiswa berhasil dihapus"; include "TampilMahasiswa.php";
}
else
{ include "TampilMahasiswa.php"; exit;
}?>
simpan dengan nama hapus.phpsimpan semua file tersebut dalam satu folder coba deh akses dari browser
Tidak ada komentar:
Posting Komentar