Namun alhamdullilah lah PHP nggak gitu sulit kok :)
Berikut program PHP input data, hapus data serta edit data, cukup simple. Bisa jadi pembelajaran dasar nih :)
Berikut program nya :)
Design Database : DBin
Nama Tabel : siswa
Fieldnya
1. id int primary key dan autoincrement
2. NIS varchar(8)
3. Nama varchar(40)
4. Alamat varchar(100)
index.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=iso-8859-1" />
<title>Contoh Sederhana Program PHP Input, Edit dan Hapus</title>
</head>
<body>
<?
mysql_connect("localhost","root","");
mysql_select_db("dbin") ;
$pil=$_GET['pil'] ;
switch($pil){
default :
echo " <input type=button value='Input Siswa' onclick=location.href='index.php?pil=input'>
<br><br>
Informasi Data Siswa SMP In Cukie
<table border=1>
<tr><td>No</td>
<td>NIS</td>
<td>Nama</td>
<td>Alamat</td>
<td>Status</td>
</tr>
" ;
$sql = "Select * from siswa " ;
$i = 0 ;
$hasil = mysql_query($sql) ;
while($row=mysql_fetch_array($hasil)){
$i++ ;
echo " <tr><td>$i</td>
<td>$row[NIS]</td>
<td>$row[Nama]</td>
<td>$row[Alamat]</td>
<td><a href='index.php?pil=edit&id=$row[id]'>Edit</a> | <a href='proses.php?mod=hapus&id=$row[id]'>Hapus</a></td>
</tr>
" ;
}
echo "</table>" ;
break ;
case "input" :
echo " Input Data Siswa <hr>
<form name=form1 method=POST action='proses.php?mod=input'>
NIS : <input type=text name=nis> <br>
Nama : <input type=text name=nama> <br>
Alamat: <input type=text name=alamat> <br>
<input type=submit name=submit value=Simpan>
<input type=reset name=submit value=Batal>
</form>
" ;
break ;
case "edit" :
$sql = "Select * from siswa where id='$_GET[id]'" ;
$hasil = mysql_query($sql) ;
$row=mysql_fetch_array($hasil) ;
echo " Edit Data Siswa <hr>
<form name=form1 method=POST action='proses.php?mod=edit'>
NIS : <input type=text name=nis value='$row[NIS]'> <br> <input type=text hidden name=id value='$row[id]'>
Nama : <input type=text name=nama value='$row[Nama]'> <br>
Alamat: <input type=text name=alamat value='$row[Alamat]'> <br>
<input type=submit name=submit value=Update>
<input type=reset name=submit value=Batal>
</form>
" ;
break ;
}
?>
</body>
</html>
proses.php
<?
mysql_connect("localhost","root","");
mysql_select_db("dbin") ;
$mod = $_GET['mod'] ;
if($mod=="input"){
$sql = "INSERT INTO siswa(NIS,
Nama,
Alamat)
VALUES('$_POST[nis]',
'$_POST[nama]',
'$_POST[alamat]')" ;
mysql_query($sql) ;
echo "<meta http-equiv='refresh' content='0; url=index.php'>";
}elseif($mod=="edit"){
$sql = "UPDATE siswa set Nama='$_POST[nama]',
Alamat='$_POST[alamat]',
NIS='$_POST[nis]'
WHERE id='$_POST[id]'" ;
mysql_query($sql) ;
echo "<meta http-equiv='refresh' content='0; url=index.php'>";
}elseif($mod=="hapus"){
$sql = "DELETE from siswa WHERE id='$_GET[id]'" ;
mysql_query($sql) ;
echo "<meta http-equiv='refresh' content='0; url=index.php'>";
}
?>
Hasilnya :
keren pak :)
ReplyDeletepenjelasan yang bapak berikan sangat bagus
ReplyDeletewalaupun saya kurang memahami fungsi dari beberapa listing kode tersebut