Input, Edit & Delete Data Mahasiswa Dengan PHP/MySQL
Assalamu’alaikum Wr.
Wb.
Salam NewGen, kali ini
kita akan belajar tentang input,edit & delete data mahasiswa dengan
PHP/MySQL.. langsung saja pertama kita buat tabelnya didatabase belajar yng
sebelumnya sudah kita buat, beri nama tbmhs dengan field-field
nim(varchar(15)), nama(varchar(35)) & email(varchar(35)) >> Simpan
Setelah kita buat tabel
mahasiswanya kita buat file-file PHPnya.
inputdata.php
Listing :
<html>
<head><title>Data Mahasiswa</title></head>
<body>
<h4>Input Data Mahasiswa</h4>
<form name="form1" method="post"
action="simpandata.php">
<table>
<tr>
<td>NIM</td><td>:</td>
<td><input
type="text" name="txtnim" id="txtnim"></td>
</tr>
<tr>
<td>Nama</td><td>:</td>
<td><input
type="text" name="txtnama"
id="txtnama"></td>
</tr>
<tr>
<td>Email</td><td>:</td>
<td><input
type="text" name="txtemail"
id="txtemail"></td>
</tr>
</table>
<p>
<label>
<input
type="submit" name="button" id="button"
value="Simpan">
</label>
</p>
</form>
<p> </p>
</body>
</html>
simpandata.php
Listing :
<!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>Sukses Simpan</title>
</head>
<body>
<a href=inputdata.php>Input Mahasiswa</a> |
<a href=daftarmhs.php>Daftar Mahasiswa</a>
<?php
$vnim=$_POST ['txtnim'];
$vnama=$_POST ['txtnama'];
$vemail=$_POST ['txtemail'];
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL = "insert into tbmhs (nim,nama,email) values
('$vnim','$vnama','$vemail')";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
?>
<p>Simpan Sukses</p>
</body>
</html>
daftarmhs.php
Listing :
<html>
<head><title>Daftar Mahasiswa</title></head>
<body>
<h4>Daftar Mahasiswa</h4>
<p>
<a href=inputdata.php>Input Mahasiswa</a> |
<a href=daftarmhs.php>Daftar Mahasiswa</a>
<p>
<?php
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL = "select * from tbmhs";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
?>
<table border="3">
<tr><td>NIM</td><td>Nama
Mahasiswa</td><td>Email</td><td>Edit</td><td>Delete</td></tr>
<?php
while($row=mysql_fetch_array ($qry))
{
echo"<tr>";
echo"<td>".$row["nim"]."</td>";
echo"<td>".$row["nama"]."</td>";
echo"<td>".$row["email"]."</td>";
echo"<td><a
href=editdata.php?nim=".$row["nim"].">Edit</a></td>";
echo"<td><a
href=deletedata.php?nim=".$row["nim"].">Delete</a></td>";
echo"<tr>";
}
?>
</table>
</p>
</body>
</html>
editdata.php
Listing :
<html>
<head><title>Data Mahasiswa</title></head>
<body>
<h4>Edit Data Mahasiswa</h4>
<?php
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL ="select * from tbmhs where nim='$_GET[nim]'";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
$row=mysql_fetch_array($qry)
?>
<form name="form1" method="post"
action="editsukses.php">
<table>
<tr>
<td>NIM</td><td>:</td>
<td><input
type="text" name="nim" id="nim" value="<?php
echo $row['nim'];?>"/></td>
</tr>
<tr>
<td>Nama</td><td>:</td>
<td><input
type="text" name="nama" id="nama"
value="<?php echo $row['nama'];?>"/></td>
</tr>
<tr>
<td>Email</td><td>:</td>
<td><input
type="text" name="email" id="email"
value="<?php echo $row['email'];?>"/></td>
</tr>
</table>
<p>
<label>
<input
type="submit" name="button" id="button"
value="Update">
</label>
</p>
</form>
<p> </p>
</body>
</html>
editsukses.php
Listing :
<html>
<head><title>Data Mahasiswa</title></head>
<body>
<h4>Sukses Update</h4>
<a href=inputdata.php>Input Mahasiswa</a> |
<a href=daftarmhs.php>Daftar Mahasiswa</a>
<?php
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL ="update tbmhs set
nama='$_POST[nama]',email='$_POST[email]' where nim='$_POST[nim]'";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
?>
</body>
</html>
deletedata.php
Listing :
<html>
<head><title>Data Mahasiswa</title></head>
<body>
<h4>Delete Data Mahasiswa</h4>
<?php
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL ="select * from tbmhs where nim='$_GET[nim]'";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
$row=mysql_fetch_array($qry)
?>
<form name="form1" method="post"
action="deletesukses.php">
<table>
<tr>
<td>NIM</td><td>:</td>
<td><input
type="text" name="nim" id="nim"
value="<?php echo $row['nim'];?>"/></td>
</tr>
<tr>
<td>Nama</td><td>:</td>
<td><input
type="text" name="nama" id="nama"
value="<?php echo $row['nama'];?>"/></td>
</tr>
<tr>
<td>Email</td><td>:</td>
<td><input
type="text" name="email" id="email"
value="<?php echo $row['email'];?>"/></td>
</tr>
</table>
<p>Anda yakin data akan di delete ?</p>
<p>
<label>
<input
type="submit" name="button" id="button"
value="Ya">
</label>
</p>
</form>
<p> </p>
</body>
</html>
deletesukses.php
Listing :
<html>
<head><title>Data Mahasiswa</title></head>
<body>
<h4>Sukses Delete</h4>
<a href=inputdata.php>Input Mahasiswa</a> |
<a href=daftarmhs.php>Daftar Mahasiswa</a>
<?php
$conn=mysql_connect("localhost","root","")
or die ("Koneksi Gagal");
mysql_select_db("belajar",$conn);
$strSQL ="delete from tbmhs where nim='$_POST[nim]'";
$qry = mysql_query($strSQL,$conn) or die ("query salah");
?>
</body>
</html>
Semua file php tadi
disimpan di folder PHP C:\xampp\htdocs\PHP
Selesai sudah, selanjutnya
kita tinggal coba caranya dengan masukkan alamat address http://localhost/PHP/inputdata.php
kemudian input data mahasiswanya
Setelah sukses simpan lihat di daftar Mahasiswa, disitu
kita bisa Edit data dan Delete Data dengan klik edit dan delete sesuai data
yang akan di edit dan delete
Terus kembangkan kreatifitas kalian dengan PHP
jangan berhenti sampai disini, akhirnya kita sudah sampai akhir pertemuan
semoga pertemuan-pertemuan selama ini bisa menbantu para NewGen dan semakin
menambah pengetahuan tentang PHP/MySQL dan bisa sebagai dasar untuk memperdalam
lagi belaja PHPnya... Selamat UAS Bagi para NewGen yang Akan melaksanakannya
ya.. Sukses Selalu
Jangan lupa yang ingin Download Source code lengkap
dan tabelnya DISINI
Salam NewGen..
Wassalamu’alaikum Wr. Wb.
No comments:
Post a Comment