Rabu, 19 Oktober 2011

TUGAS PWD 2 ( CONTOH INSERT DAN VIEW DALAM 1 FILE PHP )

Berikut adalah contoh penggunaan trigger dalam php . Yaitu berfungsi sebagai pemilih , aksi mana yang akan dilakukan

sebelumnya buat database dulu yang bernama "PWD" . Lalu jalankan perintah SQL ini untuk membuat tabel dan field nya

CREATE TABLE IF NOT EXISTS `dosen` (
`iddosen` varchar(15) NOT NULL,
`namadosen` varchar(50) NOT NULL,
`email` text NOT NULL,
PRIMARY KEY (`iddosen`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `mahasiswa` (
`nim` varchar(12) NOT NULL,
`nama` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`nim`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `matakuliah` (
`kodemk` varchar(15) NOT NULL,
`namamk` varchar(50) NOT NULL,
`bobot` varchar(25) NOT NULL,
PRIMARY KEY (`kodemk`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `pengampumatkul` (
`iddosen` varchar(15) NOT NULL,
`kodemk` varchar(15) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `wali` (
`nim` varchar(15) NOT NULL,
`iddosen` varchar(15) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



Setelah itu buat file PHP nya, dengan script dibawah ini .

<?PHP
mysql_connect("localhost","root","");
mysql_select_db("pwd");
?>
<ul>
<li><a href="?a=im">olah mahasiswa</a></li>
<li><a href="?a=id">olah dosen</a></li>
<li><a href="?a=imat">olah makul</a></li>
<li><a href="?a=wali">olah wali</a></li>
<li><a href="?a=ampu">dosen pengampu</a></li>
</ul><br><br>
<?PHP
if($_GET['aksi']=='input'){

if($_GET['p']=='mhs'){
$sql=mysql_query("INSERT INTO `mahasiswa` (
`nim` ,
`nama` ,
`email`
)
VALUES (
'$_POST[nim]', '$_POST[nama]', '$_POST[email]'
);
");
if($sql){echo "Data mahasiswa berhasil di tambahkan"."<br>";}
}

if($_GET['p']=='dosen'){
$sql=mysql_query("INSERT INTO `dosen` (
`iddosen` ,
`namadosen` ,
`email`
)
VALUES (
'$_POST[iddosen]', '$_POST[nama]', '$_POST[email]'
);
");
if($sql){echo "Data dosen berhasil di tambahkan"."<br>";}
}


if($_GET['p']=='makul'){
$sql=mysql_query("INSERT INTO `matakuliah` (
`kodemk` ,
`namamk` ,
`bobot`
)
VALUES (
'$_POST[kodemk]', '$_POST[nama]', '$_POST[bobot]'
);
");
if($sql){echo "Data mata kuliah berhasil di tambahkan"."<br>";}
}
if($_GET['p']=='wali'){
$sql=mysql_query("INSERT INTO `wali` (
`iddosen` ,
`nim`
)
VALUES (
'$_POST[iddosen]', '$_POST[nim]'
);
");
if($sql){echo "Data wali berhasil di tambahkan"."<br>";}
}
if($_GET['p']=='ampu'){
$sql=mysql_query("INSERT INTO `pengampumatkul` (
`iddosen` ,
`kodemk`
)
VALUES (
'$_POST[iddosen]', '$_POST[kodemk]'
);
");
if($sql){echo "Data pengampu berhasil di tambahkan"."<br>";}
}


}
?>
<?PHP
if($_GET['a']=='im'){?>
<form name="form1" method="post" action="index.php?aksi=input&p=mhs">
<p>Input Mahasiswa</p>
<table width="200" border="0">
<tr>
<td>NIM:</td>
<td><input type="text" name="nim" id="nim"></td>
</tr>
<tr>
<td>NAMA:</td>
<td><input type="text" name="nama" id="nama"></td>
</tr>
<tr>
<td>EMAIL:</td>
<td><input type="text" name="email" id="email"></td>
</tr>
</table>
<input type="submit" name="button" id="button" value="inputkan">
</form>
<hr>
<?PHP
$sql=mysql_query("SELECT * FROM mahasiswa order by nim asc");
while($r=mysql_fetch_array($sql)){
echo "NIM : ".$r['nim']."<br>";
echo "NAMA : ".$r['nama']."<br>";
echo "EMAIL : ".$r['email']."<br>";
echo "<br>";
}
?>
<?PHP } ?><?PHP
if($_GET['a']=='id'){?>
<form name="form1" method="post" action="index.php?aksi=input&p=dosen">
<p>Input Dosen</p>
<table width="200" border="0">
<tr>
<td nowrap>ID DOSEN:</td>
<td><input type="text" name="iddosen" id="iddosen"></td>
</tr>
<tr>
<td>NAMA:</td>
<td><input type="text" name="nama" id="nama"></td>
</tr>
<tr>
<td>EMAIL:</td>
<td><input type="text" name="email" id="email"></td>
</tr>
</table>
<input type="submit" name="button2" id="button2" value="inputkan">
</form>
<hr>
<?PHP
$sql=mysql_query("SELECT * FROM dosen order by iddosen asc");
while($r=mysql_fetch_array($sql)){
echo "ID : ".$r['iddosen']."<br>";
echo "NAMA : ".$r['namadosen']."<br>";
echo "EMAIL : ".$r['email']."<br>";
echo "<br>";
}
?>
<?PHP } ?>
<?PHP
if($_GET['a']=='imat'){?>
<form name="form1" method="post" action="index.php?aksi=input&p=makul">
<p>Input Makul</p>
<table width="200" border="0">
<tr>
<td nowrap>KODE MK:</td>
<td><input type="text" name="kodemk" id="kodemk"></td>
</tr>
<tr>
<td nowrap>NAMA:</td>
<td><input type="text" name="nama" id="nama"></td>
</tr>
<tr>
<td nowrap>BOBOT:</td>
<td><input name="bobot" type="text" id="bobot" size="5"></td>
</tr>
</table>
<input type="submit" name="button3" id="button3" value="inputkan">
</form>
<hr>
<?PHP
$sql=mysql_query("SELECT * FROM matakuliah order by kodemk asc");
while($r=mysql_fetch_array($sql)){
echo "KODE : ".$r['kodemk']."<br>";
echo "NAMA : ".$r['namamk']."<br>";
echo "BOBOT : ".$r['bobot']."<br>";
echo "<br>";
}
?>
<?PHP } ?>
<?PHP
if($_GET['a']=='wali'){?>
<form name="form1" method="post" action="index.php?aksi=input&p=wali">
<p>Input wali</p>
<table width="200" border="0">
<tr>
<td nowrap>KODE DOSEN:</td>
<td><input type="text" name="iddosen" id="iddosen"></td>
</tr>
<tr>
<td nowrap>NIM:</td>
<td><input type="text" name="nim" id="nim"></td>
</tr>

</table>
<input type="submit" name="button3" id="button3" value="inputkan">
</form>
<hr>
<?PHP
$sql=mysql_query("SELECT * FROM wali,dosen,mahasiswa where wali.iddosen=dosen.iddosen AND wali.nim=mahasiswa.nim");
while($r=mysql_fetch_array($sql)){
echo "ID : ".$r['iddosen']."<br>";
echo "NIM : ".$r['nim']."<br>";
echo "NAMA-DOSEN : ".$r['namadosen']."<br>";
echo "NAMA-MHS : ".$r['nama']."<br>";
echo "<br>";
}
?>
<?PHP } ?>
<?PHP
if($_GET['a']=='ampu'){?>
<form name="form1" method="post" action="index.php?aksi=input&p=ampu">
<p>Input Pengampu</p>
<table width="200" border="0">
<tr>
<td nowrap>KODE DOSEN:</td>
<td><input type="text" name="iddosen" id="iddosen"></td>
</tr>
<tr>
<td nowrap>KODE MAKUL:</td>
<td><input type="text" name="kodemk" id="kodemk"></td>
</tr>

</table>
<input type="submit" name="button3" id="button3" value="inputkan">
</form>
<hr>
<?PHP
$sql=mysql_query("SELECT * FROM matakuliah,dosen,pengampumatkul where matakuliah.kodemk=pengampumatkul.kodemk AND dosen.iddosen=pengampumatkul.iddosen");
while($r=mysql_fetch_array($sql)){
echo "ID : ".$r['iddosen']."<br>";
echo "KODE : ".$r['kodemk']."<br>";
echo "NAMA-DOSEN : ".$r['namadosen']."<br>";
echo "NAMA-MAKUL : ".$r['namamk']."<br>";
echo "<br>";
}
?>
<?PHP } ?>


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;
}?>