Ini menggunakan database dengan nama poltek dan ada satu kolom bernama mahasiswa dan mempunyai field nim, nama, alamat, tanggallahir, notlp.
Berikut script dari program yang telah dibuat :
<html>
<?php
mysql_connect('localhost','root','');
mysql_select_db('poltek');
function acak(){
$panjangacak = 5;
$base="ABCDEFGHKLMNOPQRSTWXYZ123456789";
$max=strlen($base)-1;
$acak="";
mt_srand((double)microtime()*1000000);
while (strlen($acak)<$panjangacak){
$acak.=$base{mt_rand(0,$max)};
}
return $acak;
}
function tambah(){
$cap1=$_POST['capcha1'];
$cap2=$_POST['capcha2'];
$nim=$_POST['nim'];
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$tgl=$_POST['lahir'];
$notlp=$_POST['tlp'];
if($cap1==$cap2){
$q=mysql_query("insert into mahasiswa values ('$nim','$nama','$alamat','$tgl','$notlp')") or die (mysql_error());
if($q){
echo "<h4><center>Berhasil Menambah Data</center></h4>";
}
}else {
echo "<h4><center>Captcha Tidak cocok</center></h4>";
}
}
function update(){
$nim=$_POST['nim'];
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$tgl=$_POST['lahir'];
$notlp=$_POST['tlp'];
$y=mysql_query("update mahasiswa set nama='$nama', alamat='$alamat', tanggallahir='$tgl', notlp='$notlp' where nim='$nim' ");
if ($y){
header("location:uas.php");
}
}
if (isset($_POST['tambah'])){
echo tambah();
}
if (isset($_POST['update'])){
echo update();
}
if (isset($_GET['delete'])){
$nimku=$_GET['delete'];
mysql_query("delete from mahasiswa where nim='$nimku'");
}
?>
<head>
<style type="text/css">
#capcha{
color:#FFFFFF;
background-color:#999999;
font-size:17px;
font-family:Algerian;
width:80px;
height:38px;
margin-bottom:5px;
padding-left:11px;
}
</style>
<title>UAS WEB II</title>
</head>
<body bgcolor="#CC9966"><center>
<?php
if (isset($_GET['edit'])){
$new = $_GET['edit'];
$ya=mysql_query("select * from mahasiswa where nim='$new'");
$oke=mysql_fetch_array($ya);
echo '<hr>';
echo '<h2> Edit Data</h2>';
echo '<hr>';
echo '<form action="" method="post">
<input type="hidden" name="nim" value="'.$new.'" />
Nim <br> <input type="text" value="'.$oke['nim'].'" disabled/><br>
Nama <br> <input type="text" name="nama" value="'.$oke['nama'].'" /><br>
Alamat <br> <input type="text" name="alamat" value="'.$oke['alamat'].'" /><br>
Tanggal lahir <br> <input type="text" name="lahir" value="'.$oke['tanggallahir'].'" /><br>
<font size="2" color="#FF0000">Contoh : 1999 - 12 - 23</font> <br>
No. Telp <br> <input type="text" name="tlp" value="'.$oke['notlp'].'" /><br>
<button type="submit" name="update" value="update" >Update</button>
</form>
<a href="uas.php"><button type="button">Tambah Data</button><a>
';
}else{
echo '<hr>';
echo '<h2> Tambah Data</h2>';
echo '<hr>';
echo '<form action="" method="post">
Nim <br> <input type="text" name="nim" /><br>
Nama <br> <input type="text" name="nama" /><br>
Alamat <br> <input type="text" name="alamat" /><br>
Tanggal lahir <br> <input type="text" name="lahir" /><br>
<font size="2" color="#FF0000">Contoh : 1999 - 12 - 23</font><br>
No. Telp <br> <input type="text" name="tlp" /><br>
Captcha <br>
<input id="capcha" type="text" name="capcha1" value="'.acak().'" readonly/><br>
<input type="text" name="capcha2" /><br>
<button type="submit" name="tambah" value="tambah" >TAMBAH</button>
</form>';
}
?>
<table width="761" height="76" border="1">
<tr bgcolor="#009999">
<td width="87"><center>Nim</center></td>
<td width="157"><center>Nama</center></td>
<td width="107"><center>Alamat</center></td>
<td width="114"><center>Tanggal Lahir</center></td>
<td width="130"><center>No. Telp</center></td>
<td width="150"><center>Aksi</center></td>
</tr>
<?php
$a=mysql_query("select * from mahasiswa");
while ($b=mysql_fetch_array($a)){
?>
<tr bgcolor="#CCCCCC">
<td><center><?php echo $b['nim']; ?></center></td>
<td><?php echo $b['nama']; ?></td>
<td><?php echo $b['alamat']; ?></td>
<td><center><?php echo $b['tanggallahir']; ?></center></td>
<td><center><?php echo $b['notlp']; ?></center></td>
<td><center><a href="?edit=<?php echo $b['nim']; ?>" ><input type="submit" value="EDIT"></a> <a href="?delete=<?php echo $b['nim']; ?>"><input type="submit" value="HAPUS"></a></center></td>
</tr>
<?php
}
?>
</table>
</center>
</body>
</html>
Tampilannya akan seperti ini :
Terima Kasih :D
Tidak ada komentar:
Posting Komentar