Simple PHP & MySQL Script… (Continue)

Posted: 11 February 2011 in Catatan Kuliah, Learning, Programming, Sistem Informasi
Tags: , ,

Bagi yang baru belajar PHP & MySQL, berikut contoh sederhana program manipulasi secara lengkap, mulai dari Tambah data, Edit data dan menghapus data. Lebih lengkapnya silahkan di copas….đŸ™‚

Jangan lupa untuk me-rename file menjadi pratikum.zip.pdf agar bisa di ektrak…

Selamat mencoba…

 

Kode untuk View :

<html>
<head>
<title>View Mahasiswa</title>
</head>

<body>
<?php
	$Conn = mysql_pconnect("localhost" ,"root", "");
	mysql_select_db("dbAkademik");
	
	$sql="SELECT * FROM Mahasiswa ORDER BY NIM ASC"; 
	$kueri=mysql_query($sql,$Conn) or die (mysql_error());
?>

<table width="790" border="1" align="center" cellspacing="0" >
  <tr>
    <th width="5%">No</th>
    <th width="5%">NIM</th>
    <th width="9%">Nama</th>
    <th width="20%">Jurusan</th>
    <th width="3%" colspan="3">Menu</th>
  </tr>
<?
	$i=1;
	while ($row = mysql_fetch_assoc($kueri)) {
?>
  <tr>
    <th><?php echo $i; $i++; ?></th>
    <td><?php echo $row['NIM']; ?></td>
    <td><?php echo $row['Nama']; ?></td>
    <td><?php echo $row['Jurusan']; ?></td>
    <td align="center"><?php echo "<a href='MhsFormEdit.php?nim=$row[NIM]'><img src='img/edit.jpg'></a>" ?></td>
    <td align="center"><?php echo "<a href='MhsFormDel.php?nim=$row[NIM]'><img src='img/del.jpg'></a>" ?></td>
    <td align="center"><?php echo "<a href='MhsFormTambah.php'><img src='img/add.jpg'></a>" ?></td>
  </tr>
<? } ?>
</table>

</body>
</html>

 

Kode untuk Edit Form :

<html>
<head>
<title>AKADEMIK</title>
</head>

<body>
<?php
	$Conn = mysql_pconnect("localhost" ,"root", "");
	mysql_select_db("dbAkademik");
	
	$nim=$_GET['nim'];
	$sql="SELECT * FROM Mahasiswa WHERE nim='$nim'"; 
	$kueri=mysql_query($sql,$Conn) or die (mysql_error());
	$row = mysql_fetch_array($kueri);
?>

<form action="MhsKonfirmasiEdit.php" method="post"> 
<table width="50%" align="center"> 
	<tr><td>NIM</td><td> <input name="nim" type="text" id="nim" size="15" maxlength="10" value="<?php echo $row[0]; ?>" /></td></tr>
	<tr><td>Nama</td><td><input name="nama" type="text" id="nama" size="60" maxlength="60" value="<?php echo $row[1]; ?>" /></td></tr>
	<tr><td>Jurusan</td><td>
		<select name="jurusan" id="jurusan">
            <option selected><?php echo $row[2];?></option>
			<option>SI - Sistem Informasi</option>
			<option>TI - Teknik Informatika</option>
			<option>MI - Manajemen Informatika</option>
			<option>KA - Komputer Akuntansi</option>
		</select>
	</td></tr>
	<tr><td>Asal Sekolah </td><td><input name="sekolah" type="text" id="sekolah" size="60" maxlength="60" value="<?php echo $row[3]; ?>" /></td></tr>
	<tr><td>&nbsp; </td><td><input type="hidden" name="npm" value="<?php echo $row[0]; ?>"><input name="submit" type="submit" value="   Kirim   " />
</form>
<form action="MhsView.php"><input name="submit" type="submit" value="   Batal   " /></form></td></tr>
</table>
</body>
</html>

 

Kode untuk Konfirmasi Edit :

<?
	$nim=$_POST['nim'];
	$nama=$_POST['nama'];
	$jurusan=$_POST['jurusan'];
	$sekolah=$_POST['sekolah'];
			
			
	$Conn = mysql_pconnect("localhost" ,"root", "");
	mysql_select_db("dbAkademik");
	$query="UPDATE `mahasiswa` SET `NIM`='$nim',`Nama`='$nama',`Jurusan`='$jurusan',`AsalSekolah`='$sekolah' where nim='$nim'";
	$result=mysql_query($query, $Conn) or die (mysql_error());
	if($result){
		header("location:MhsView.php");
	}
	else{
		echo "<br><br><center>Maaf, Anda tidak berhasil memasukan data.<br>";
		echo "<center>[<a href='MahasiswaForm.php'>Kembali</a>] ke Form.";
	}
?>

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s