Rabu, 12 Oktober 2011

Contoh sederhana - CRUD - Create , Update , Delete

Pembuatan Database


CREATE TABLE IF NOT EXISTS `mapel` (
  `id_mapel` int(11) NOT NULL AUTO_INCREMENT,
  `nama_mapel` varchar(50) NOT NULL,
  PRIMARY KEY (`id_mapel`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;


File koneksi.php


<?PHP
$host='localhost';

$pass='';

$id='root';

$data='basdat';
mysql_connect($host,$id,$pass) or die ('Tidak terkoneksi dengan database');

mysql_select_db($data) or die ('Database not found');

?>
 File Gateway.php


<?PHP switch($_REQUEST['a']){

//Awal syntax<br />

case'':include'home.php';break;

case'home':include'home.php';break;

case'makul':include'makul.php';break;

}

?>
 File Nilai.php



<?PHP include'config/koneksi.php'; ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sistem materi dosen</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div class="menu">
<ul>
<a href="?a=makul"><li>Lihat Daftar Ma.Kul</li> </a>
<a href="?a=home"><li>Lihat Daftar Materi</li></a>
</ul>
</div>
<div class="clear"></div>
<div class="content">
<?PHP include 'gateway.php'; ?>
</div>
</div>
</body>
</html>
 File Makul.php


<?PHP switch($_REQUEST['act']){

default:

?><script type="text/javascript"><!--

function pindah(){location.href = '?a=makul&act=tambah';}
// --></script>

<h2>List Materi Kuliah Yang Tersedia</h2>
<input type="button" value="Tambah Mata Kuliah" onclick="pindah()"/>

<br /> <hr>
<table width="59%" border="1">

<tr>

<th width="579">Nama Mata Kuliah</th>

<th width="42" nowrap>Aksi</th>

</tr>

<?PHP $baru=mysql_query("Select * from mapel ");

while($z=mysql_fetch_array($baru)){

?><tr>

<td><?PHP echo $z['nama_mapel']; ?></td>

<td nowrap><a href="?a=makul&act=edit&id=<?php echo $z['id_mapel']; ?>"><img alt="Edit" src="images/edit.png" title="Edit"/></a>|<a onclick='return confirm("Apakah anda yakin ingin menghapus data <?PHP echo $z['nama_mapel'];?> ?")' href="?a=makul&act=hapus&id=<?php echo $z['id_mapel']; ?>" ><img src="images/delete.png" alt="delete" title="Delete"/></a></td>



</tr> <?PHP } ?>

</table>

<?PHP

break;

case'tambah':

if(empty($_GET['makul'])){

echo"

<script language='javascript'>

var tanya=prompt('Nama Makul','');

location.href = '?a=makul&act=tambah&makul='+tanya;

</script>

";

}

else{

switch($_REQUEST['makul']){

default:

if ($_GET['makul']=="null"){

echo"<script language='javascript'>

location.href = '?a=makul';

</script>";

}else{

$insert=mysql_query("INSERT INTO `mapel` (

`nama_mapel`

)

VALUES (

'$_GET[makul]'

)");

echo"<script language='javascript'>

alert('Makul $_GET[makul] berhasil ditambahkan');

location.href = '?a=makul';

</script>";

}

break;

}

}
break;
case'edit':

if(empty($_GET['makul'])){

$baru=mysql_query("Select * from mapel where id_mapel='$_GET[id]' ");

$m=mysql_fetch_array($baru);

echo"

<script language='javascript'>

var tanya=prompt('Nama Makul','$m[nama_mapel]');

location.href = '?a=makul&act=edit&id=$_GET[id]&makul='+tanya;

</script>

";}else{

switch($_REQUEST['makul']){


default:

if($_GET['makul']=="null"){

echo"<script language='javascript'>location.href ='?a=makul';</script>";

}else{

$query=mysql_query("UPDATE `mapel` SET `nama_mapel` = '$_GET[makul]' where id_mapel=$_GET[id]");

echo"<script language='javascript'>

alert('Makul $_GET[makul] berhasil diupdate');

location.href ='?a=makul';

</script>";

}

break;
}

}

break;

case'hapus':

$hapus=mysql_query("DELETE FROM `mapel` WHERE id_mapel='$_GET[id]'");

echo"<script language='javascript'>

alert('Mata kuliah berhasil dihapus');

location.href = '?a=makul';

</script>";

break;
}?>


1 komentar:

Unknown mengatakan...

mantap gan,,,,,

Posting Komentar