Cara Membuat Form Entry Data, Edit/Update, Hapus Di PHP
Pertama yang perlu diketahui yaitu bagaimana membuat database dengan nama " data_siswa" dan tabel " mhs", yang berisi field nim,nama, alamat, agama dan nomor telepon. Atau inport saja code sql dibawah ini kedalam phpmyadmin anda agar lebih cepat dan mudah.
-- phpMyAdmin SQL Dump
-- version 2.11.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 16, 2013 at 09:48 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `data_siswa`
--
-- --------------------------------------------------------
--
-- Table structure for table `mhs`
--
CREATE TABLE `mhs` (
`nim` int(5) NOT NULL auto_increment,
`nama` varchar(20) collate latin1_general_ci default NULL,
`alamat` varchar(30) collate latin1_general_ci default NULL,
`agama` varchar(10) collate latin1_general_ci default NULL,
`notlpn` varchar(10) collate latin1_general_ci default NULL,
PRIMARY KEY (`nim`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
--
-- Dumping data for table `mhs`
--
-- version 2.11.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 16, 2013 at 09:48 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `data_siswa`
--
-- --------------------------------------------------------
--
-- Table structure for table `mhs`
--
CREATE TABLE `mhs` (
`nim` int(5) NOT NULL auto_increment,
`nama` varchar(20) collate latin1_general_ci default NULL,
`alamat` varchar(30) collate latin1_general_ci default NULL,
`agama` varchar(10) collate latin1_general_ci default NULL,
`notlpn` varchar(10) collate latin1_general_ci default NULL,
PRIMARY KEY (`nim`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
--
-- Dumping data for table `mhs`
--
Setelah langkah pembuatan database selesai maka yang perlu dibuat yaitu buat folder baru di server local semisal wampserver.seperti ini caranya masuk ke local disk c cari Xampp buat seperti berikut Xampp/htdoch/namafolder yang akan dibuat. Kemudian buat file koneksi.php untuk koneksi ke database.
<?php
$host="localhost";
$user="root";
$pass="";
$db="data_siswa";
$sambung=mysql_connect($host,$user,$pass);
mysql_select_db($db,$sambung);
?>
$host="localhost";
$user="root";
$pass="";
$db="data_siswa";
$sambung=mysql_connect($host,$user,$pass);
mysql_select_db($db,$sambung);
?>
Kita selanjutnya akan membuat index.php yang digunakan sebagai file utama sbb:
<html>
<head>
<title>Daftar Mahasiswa</title>
</head>
<body>
<?php
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_siswa") or die ("Gagal membuka database.");
?>
<table border="5">
<tr>
<th>NIM</th>
<th>Nama Mahasiswa</th>
<th>Alamat</th>
<th>Agama</th>
<th>No_tlpn</th>
<th colspan="3">Aksi</th>
</tr>
<?php
$query = "select * from mhs";
$result = mysql_query($query, $sambung);
//$no = 0;
while ($buff = mysql_fetch_array($result)){
//$no++;
?>
<tr>
<td><?php echo $buff['nim']; ?></td>
<td><?php echo $buff['nama']; ?></td>
<td><?php echo $buff['alamat']; ?></td>
<td><?php echo $buff['agama']; ?></td>
<td><?php echo $buff['notlpn']; ?></td>
<td><a href="edit.php?nim=<?php echo $buff['nim']; ?>">Edit</a></td>
<td><a href="hapus.php?nim=<?php echo $buff['nim']; ?>">Hapus</a></td>
</tr>
<?php
}
mysql_close($sambung);
?>
</table>
<p align="left"><a href="tambah.html">Tambah Data</a></p>
</body>
</html>
<head>
<title>Daftar Mahasiswa</title>
</head>
<body>
<?php
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_siswa") or die ("Gagal membuka database.");
?>
<table border="5">
<tr>
<th>NIM</th>
<th>Nama Mahasiswa</th>
<th>Alamat</th>
<th>Agama</th>
<th>No_tlpn</th>
<th colspan="3">Aksi</th>
</tr>
<?php
$query = "select * from mhs";
$result = mysql_query($query, $sambung);
//$no = 0;
while ($buff = mysql_fetch_array($result)){
//$no++;
?>
<tr>
<td><?php echo $buff['nim']; ?></td>
<td><?php echo $buff['nama']; ?></td>
<td><?php echo $buff['alamat']; ?></td>
<td><?php echo $buff['agama']; ?></td>
<td><?php echo $buff['notlpn']; ?></td>
<td><a href="edit.php?nim=<?php echo $buff['nim']; ?>">Edit</a></td>
<td><a href="hapus.php?nim=<?php echo $buff['nim']; ?>">Hapus</a></td>
</tr>
<?php
}
mysql_close($sambung);
?>
</table>
<p align="left"><a href="tambah.html">Tambah Data</a></p>
</body>
</html>
File utama sudah jadi sekarang fungsi untuk memasukan data buat dengan nama entry.php sbb:
<?php
include("koneksi.php");
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$agama = $_POST['agama'];
$notlpn = $_POST['notlpn'];
$query = mysql_query("insert into mhs values ('$nim','$nama','$alamat','$agama','$notlpn')");
echo "Data Telah disimpan<br>
<a href=\"index.php\">Kembali</a>";
?>
include("koneksi.php");
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$agama = $_POST['agama'];
$notlpn = $_POST['notlpn'];
$query = mysql_query("insert into mhs values ('$nim','$nama','$alamat','$agama','$notlpn')");
echo "Data Telah disimpan<br>
<a href=\"index.php\">Kembali</a>";
?>
Kita perlu membuat file tampilan tambah data dengan nama tambah.html sbb:
<html>
<head><title>Tambah Data</title></head>
<body>
<h3><p align="left">Tambah Data Mahasiswa</p></h3>
<form name="form1" method="post" action="entry.php">
<table>
<tr>
<td>NIM</td>
<td><input type="text" name="nim" size="12"></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" size="30"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" size="50"></td>
</tr>
<tr>
<td>Agama</td>
<td><input type="text" name="agama" size="10"></td>
</tr>
<tr>
<td>No. Tlpn</td>
<td><input type="text" name="notlpn" size="15"></td>
</tr>
<tr>
<td><input value="Simpan" name="submit" type="submit" /></td>
</tr>
<tr>
<td><input value="ulangi" type="reset" /></td>
</tr>
<tr>
<td><input value="Kembali" type="button" onClick="self.history.back()"></td>
</tr>
</table>
</form>
</body>
</html>
<head><title>Tambah Data</title></head>
<body>
<h3><p align="left">Tambah Data Mahasiswa</p></h3>
<form name="form1" method="post" action="entry.php">
<table>
<tr>
<td>NIM</td>
<td><input type="text" name="nim" size="12"></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" size="30"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" size="50"></td>
</tr>
<tr>
<td>Agama</td>
<td><input type="text" name="agama" size="10"></td>
</tr>
<tr>
<td>No. Tlpn</td>
<td><input type="text" name="notlpn" size="15"></td>
</tr>
<tr>
<td><input value="Simpan" name="submit" type="submit" /></td>
</tr>
<tr>
<td><input value="ulangi" type="reset" /></td>
</tr>
<tr>
<td><input value="Kembali" type="button" onClick="self.history.back()"></td>
</tr>
</table>
</form>
</body>
</html>
Fungsi update data dengan file update.php sbb:
<?php
include("koneksi.php");
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$agama = $_POST['agama'];
$notlpn = $_POST['notlpn'];
$query = mysql_query("update mhs set nama='$nama', alamat='$alamat', agama='$agama',notlpn='$notlpn' where nim='$nim'");
echo "Data Telah diupdate<br>
<a href=\"index.php\">Kembali</a>";
?>
include("koneksi.php");
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$agama = $_POST['agama'];
$notlpn = $_POST['notlpn'];
$query = mysql_query("update mhs set nama='$nama', alamat='$alamat', agama='$agama',notlpn='$notlpn' where nim='$nim'");
echo "Data Telah diupdate<br>
<a href=\"index.php\">Kembali</a>";
?>
Buat Fungsi hapus data dengan file hapus.php sbb:
<?php
include("koneksi.php");
mysql_query("DELETE from mhs WHERE nim='$_GET[nim]'");
echo"Data Telah dihapus<br>
<a href=\"index.php\">Kembali</a>";
?>
include("koneksi.php");
mysql_query("DELETE from mhs WHERE nim='$_GET[nim]'");
echo"Data Telah dihapus<br>
<a href=\"index.php\">Kembali</a>";
?>
Terakhir buat fungsi edit dengan file name edit.php sbb:
<?php
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_siswa") or die ("Gagal membuka database.");
//$edit=mysql_query("SELECT * FROM mhs WHERE nim='$_GET[nim]'");
//$r_edit=mysql_fetch_array($edit);
$nim = $_GET['nim'];
$query = "select * from mhs where nim='$nim'";
$result = mysql_query($query, $sambung) or die("gagal melakukan query");
$buff = mysql_fetch_array($result);
mysql_close($sambung);
?>
<html>
<head><title>Edit Data</title></head>
<body>
<form name="form1" method="post" action="update.php">
<table>
<tr>
<td>Nim</td><td><input type="text" name="nim" value="<?php echo $buff['nim']; ?>"></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" value="<?php echo $buff['nama']; ?>"></td></tr>
<tr><td>Alamat</td><td><input type="text" name="alamat" value="<?php echo $buff['alamat']; ?>" size="50"></td></tr>
<tr><td>Agama</td><td><input type="text" name="agama" value="<?php echo $buff['agama']; ?>"></td></tr>
<tr><td>No. tlpn</td><td><input type="text" name="notlpn" value="<?php echo $buff['notlpn']; ?>"></td></tr>
<tr>
<input value="Simpan" type="submit" name="submit"/>
<input type="button" value="Kembali" onClick="self.history.back()"></td></tr>
</table>
</form>
</body>
</html>
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_siswa") or die ("Gagal membuka database.");
//$edit=mysql_query("SELECT * FROM mhs WHERE nim='$_GET[nim]'");
//$r_edit=mysql_fetch_array($edit);
$nim = $_GET['nim'];
$query = "select * from mhs where nim='$nim'";
$result = mysql_query($query, $sambung) or die("gagal melakukan query");
$buff = mysql_fetch_array($result);
mysql_close($sambung);
?>
<html>
<head><title>Edit Data</title></head>
<body>
<form name="form1" method="post" action="update.php">
<table>
<tr>
<td>Nim</td><td><input type="text" name="nim" value="<?php echo $buff['nim']; ?>"></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" value="<?php echo $buff['nama']; ?>"></td></tr>
<tr><td>Alamat</td><td><input type="text" name="alamat" value="<?php echo $buff['alamat']; ?>" size="50"></td></tr>
<tr><td>Agama</td><td><input type="text" name="agama" value="<?php echo $buff['agama']; ?>"></td></tr>
<tr><td>No. tlpn</td><td><input type="text" name="notlpn" value="<?php echo $buff['notlpn']; ?>"></td></tr>
<tr>
<input value="Simpan" type="submit" name="submit"/>
<input type="button" value="Kembali" onClick="self.history.back()"></td></tr>
</table>
</form>
</body>
</html>
Setelah semua file dibuat jalan kan localhost dan ketik dengan format localhost/nama folder/index.php.
Selamat berjuang membuat fungsi edit, tambah, hapus di php.
Semoga sukses.
No comments:
Post a Comment