<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p><a href="tampil.php">Tampil database</a> / <a href="tambah.php">Tambah Data</a></p>
<h1>Edit Data Mahasiswa
<?php
include('koneksi.php');
$id = $_GET['id'];
$show = mysql_query("SELECT * FROM addmahasiswa WHERE mhs_id='$id'");
if(mysql_num_rows($show) == 0){
echo '<script>window.history.back()</script>';
}else{
$data = mysql_fetch_assoc($show);
}
?>
<form action="edit-proses.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<table cellpadding="3" cellspacing="0">
<tr bgcolor="#FF1493">
<td>NIM</td>
<td>:</td>
<td><input type="text" name="nim" value="<?php echo $data['nim']; ?>" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" value="<?php echo $data['nama']; ?>" required></td>
</tr>
<tr bgcolor="#B22222">
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" value="<?php echo $data['alamat']; ?>" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" value="<?php echo $data['email']; ?>" required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="simpan" value="Simpan"></td>
</tr>
</table>
</form></h1>
</body>
</html>
edit-proses.php
<?php
if(isset($_POST['simpan'])){
include('koneksi.php');
$id = $_POST['id'];
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$email = $_POST['email'];
$update = mysql_query("UPDATE addmahasiswa SET nim='$nim', nama='$nama', alamat='$alamat', email='$email' WHERE mhs_id='$id'") or die(mysql_error());
if($update){
echo '<center><h1>Data berhasil di simpan! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}else{
echo 'Gagal menyimpan data! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}
}else{
echo '<script>window.history.back()</script>';
}
?>
hapus.php
<?php
if(isset($_GET['id'])){
include('koneksi.php');
$id = $_GET['id'];
$cek = mysql_query("SELECT mhs_id FROM addmahasiswa WHERE mhs_id='$id'") or die(mysql_error());
if(mysql_num_rows($cek) == 0){
echo '<script>window.history.back()</script>';
}else{
$del = mysql_query("DELETE FROM addmahasiswa WHERE mhs_id='$id'");
if($del){
echo '<center><h1>Data mahasiswa berhasil di hapus! ';
echo '<a href="tampil.php">Kembali</a>';
}else{
echo 'Gagal menghapus data! ';
echo '<a href="tampil.php">Kembali</a>';
}
}
}else{
echo '<script>window.history.back()</script>';
}
?>
koneksi.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$name = "databasepwd5";
$koneksi = mysql_connect($host, $user, $pass) or die("Koneksi ke database gagal!");
mysql_select_db($name, $koneksi) or die("Tidak ada database yang dipilih!");
?>
tambah.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p> <a href="tampil.php">Tampil Database gan!</a><br>
<a href="tambah.php">Tambah Data gan!</a>
</p>
<br>
<h1>Tambah Data Mahasiswa
<form action="tambah-proses.php" method="post">
<table cellpadding="3" cellspacing="0">
<tr bgcolor="#FF1493">
<td>Nim</td>
<td>:</td>
<td><input type="text" name="nim" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" required></td>
</tr>
<tr bgcolor="#B22222">
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" required></td>
</tr>
<tr bgcolor="#1E90FF">
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" required></td>
</tr>
<tr>
<td> </td>
<td></td>
<td><input type="submit" name="tambah" value="Tambah"></td>
</tr>
</table>
</form></h1>
</body>
</html>
tambah-proses.php
<?php
if(isset($_POST['tambah'])){
include('koneksi.php');
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$email = $_POST['email'];
$input = mysql_query("INSERT INTO addmahasiswa VALUES(NULL, '$nim', '$nama', '$alamat', '$email')") or die(mysql_error());
if($input){
echo '<center><h1>Data berhasil di tambahkan! ';
echo '<a href="tambah.php"><h1>Kembali</h1></a>';
}else{
echo '<h1>Gagal menambahkan data! </h1>';
echo '<a href="tambah.php"><h1>Kembali</a></h1>';
}
}else{
echo '<script>window.history.back()</script>';
}
?>
tampil.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<p> <a href="tampil.php">Tampil Database gan!</a><br>
<a href="tambah.php">Tambah Data gan! </a>
</p>
<br>
<h1>Data Mahasiswa
<table border="2" class="table">
<tr bgcolor="#FF1493">
<th>No</th>
<th>Nim</th>
<th>Nama</th>
<th>Alamat</th>
<th>Email</th>
<th>Opsi</th>
</tr></h1>
<?php
include "koneksi.php";
$query_mysql=mysql_query("SELECT * FROM addmahasiswa")or die(mysql_error());
$no=1;
while($data = mysql_fetch_assoc($query_mysql)){
?>
<tr bgcolor="#E9967A">
<td><?php echo $no++; ?></td>
<td><?php echo $data['nim']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['alamat']; ?></td>
<td><?php echo $data['email']; ?></td>
<td>
<a class="edit" href="edit.php?id=<?php echo $data['mhs_id'];?>">Edit</a> |
<a class="hapus" href="hapus.php?id=<?php echo $data['mhs_id'];?>">Hapus</a>
</td>
</tr>
<?php }?>
</table>
</body>
</html>
Tidak ada komentar:
Posting Komentar