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


0 komentar:

Posting Komentar