Thursday, December 19, 2013

Cara membuat view, tambah, edit, hapus pada PHP

 

Assalamualaikum gan
Berikut sedikit materi tentang php untuk  membuat view, tambah, edit, hapus pada PHP
Jangan buat database dengan nama mid. Kemudian di dalam database mid anda harus membuat sebuah tabel bernama nilai.
Adapun strukturnya adalah sebagai berikut:

Nama field     Type
nis                 Char(3)
nama             Varchar(30)
nilai               Decimal(6,2)

Berikut Scripnya !!!

Nama file: index.php

<html> 

<head> 

<title>Menampilkan Data</title> 

</head> 

<body> 

<?php 

$koneksi = mysql_connect("localhost", "root", "") or die("Gagal konek ke server."); 

mysql_select_db("mid") or die("Gagal membuka database."); 

?> 

<p><a href="tambah.php">Tambah data</a></p> 

<table border="1"> 

<tr align="center">

<td>NIS</td>

<td>Nama</td>

<td>Nilai</td>

<td>Action</td>

</tr>

<?php

$query = "select * from nilai";

$hasil = mysql_query($query, $koneksi) or die("Gagal melakukan query.");

while ($buff = mysql_fetch_array($hasil)) {

?>

<tr>

<td><?php echo $buff['nis']; ?></td>

<td><?php echo $buff['nama']; ?></td>

<td><?php echo $buff['nilai']; ?></td>

<td><a href="edit.php?nis=<?php echo $buff['nis']; ?>">Edit</a> |

<a href="hapus.php?nis=<?php echo $buff['nis']; ?>">Hapus</a></td>

</tr>

<?php

};

mysql_close($koneksi);

?>

</table>

</body>

</html> 

Nama file: tambah.php

<html>
<head>
<title>Menambah Data</title>

</head>
<body>
<form name="form1" method="post" action="tambah2.php">

<table border="1">

<tr>
<td>NIS</td>
<td>:</td>
<td><input name="nis" type="text" /></td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><input name="nama" type="text" /></td>
</tr>
<tr>
<td>Nilai</td>
<td>:</td>
<td><input name="nilai" type="text" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Simpan" /></td>
</tr>
</table>
</form>
<p>Batal menambah data. Kembali ke <a href="index.php">index.php</a></p>
</body>
</html> 


Nama file: tambah2.php

<html>

<head>
<title>Menambah Data</title>

</head>
<body>
<?php

$koneksi = mysql_connect("localhost", "root", "") or die("Gagal konek ke server.");

mysql_select_db("mid") or die("Gagal membuka database.");

$nis = $_POST['nis'];

$nama = $_POST['nama'];

$nilai = $_POST['nilai'];

$query = mysql_query("insert into nilai values ('$nis', '$nama', '$nilai')");

mysql_close($koneksi);
?>

<p>Data sudah tersimpan. Silakan dicek di <a href="index.php">index.php</a></p>

</body>
</html>

Nama file: edit.php

<html>

<head>

<title>Mengedit Data</title>

</head>
<body>
<?php
$koneksi = mysql_connect("localhost", "root", "") or die("Gagal konek ke server.");
mysql_select_db("mid") or die("Gagal membuka database.");
$nis = $_GET['nis'];
$query = "select * from nilai where nis='$nis'";
$hasil = mysql_query($query, $koneksi) or die("Gagal melakukan query.");
$buff = mysql_fetch_array($hasil);
mysql_close($koneksi);
?>
<form name="form1" method="post" action="edit2.php">
<input type="hidden" name="nis" value="<?php echo $buff['nis']; ?>" />
<table border="1">
<tr>
<td>NIS</td>
<td>:</td>
<td><?php echo $buff['nis']; ?></td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><input name="nama" type="text" value="<?php echo $buff['nama']; ?>" /></td>
</tr>
<tr>
<td>Nilai</td>
<td>:</td>
<td><input name="nilai" type="text" value="<?php echo $buff['nilai']; ?>" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Simpan" /></td>
</tr>
</table>
</form>
<p>Batal mengedit data. Kembali ke <a href="index.php">index.php</a></p>
</body>
</html>


Nama file: edit2.php

<html>

<head>

<title>Mengedit Data</title>

</head>

<body>

<?php

$koneksi = mysql_connect("localhost", "root", "") or die("Gagal konek ke server.");

mysql_select_db("mid") or die("Gagal membuka database.");

$nis = $_POST['nis'];

$nama = $_POST['nama'];

$nilai = $_POST['nilai'];

$query = mysql_query("update nilai set nama='$nama', nilai='$nilai' where nis='$nis'");

mysql_close($koneksi);

?>

<p>Data sudah ter-update. Silakan dicek di <a href="index.php">index.php</a></p>

</body>

</html>

Nama file: hapus.php

<html>

<head>

<title>Menghapus Data</title>

</head>

<body>

<?php

$koneksi = mysql_connect("localhost", "root", "") or die("Gagal konek ke server.");

mysql_select_db("mid") or die("Gagal membuka database.");

$nis = $_GET['nis'];

mysql_query("delete from nilai where nis='$nis'") or die("Gagal menghapus data.");

mysql_close($koneksi);

?>

<p>Data telah dihapus. Silakan dicek di <a href="index.php">index.php</a></p>

</body>

</html>

Silahkan mencoba..

No comments:

Post a Comment