Assalamualaikum Wr. Wb.
Hari ini saya akan berbagi tentang pembuatan form edit,hapus dan tambah pada PHP MySQl
yang di hubungkan dengan database di sini saya menggunakan data base
puskesmas yang sudah saya buat kemarin contohnya di postingan saya
kemarin.
Tutorial :
1. Buatlah database bernama puskesmas di dalam database tersebut buatlah 5
tabel yang bernama (pasien,dokter,kamar,jenis_layanan,medical_record)
dan
buatlah script phpnya dengan nama puskesmas.php simpan di
var/www
/html/Formulir/ bila anda menggunakan linux.
Bila anda bingung klik saja link ini :http://febriimammustafid.blogspot.co.id/2016/04/membuat-form-insert-pada-php-mysql.html
2. Buatlah 4 (empat) file php berikut ini cara membuat file php.
a. Buat dengan nama dbconfig.php simpan di file direktori var/www
/html/Formulir/ copy
saja script di bawah ini.
<?php
$host = "localhost";
$user = "root";
$password = "mustafid123";
$datbase = "puskesmas";
mysql_connect($host,$user,$password);
mysql_select_db($datbase);
?>
b. Buat dengan nama edit_data.php simpan di file direktori var/www
/html/Formulir/ copy
saja script di bawah ini.
<?php
include_once 'dbconfig.php';
if(isset($_GET['edit_id']))
{
$sql_query="SELECT * FROM pasien WHERE kode_pasien=".$_GET['edit_id'];
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
// variables for input data
$kode_pasien = $_POST['kode_pasien'];
$nama_pasien = $_POST['nama_pasien'];
$jenis_kelamin = $_POST['jenis_kelamin'];
$gol_darah = $_POST['gol_darah'];
$umur = $_POST['umur'];
$alamat = $_POST['alamat'];
// variables for input data
// sql query for update data into database
$sql_query = "UPDATE pasien SET
kode_pasien='$kode_pasien',nama_pasien='$nama_pasien',jenis_kelamin='$jenis_kelamin',gol_darah='$gol_darah',umur='$umur',alamat='$alamat'
WHERE kode_pasien=".$_GET['edit_id'];
// sql query for update data into database
// sql query execution function
if(mysql_query($sql_query))
{
?>
<script type="text/javascript">
alert('Data Are Updated Successfully');
window.location.href='index.php';
</script>
<?php
}
else
{
?>
<script type="text/javascript">
alert('error occured while updating data');
</script>
<?php
}
// sql query execution function
}
if(isset($_POST['btn-cancel']))
{
header("Location: update.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>CRUD Operations With PHP and MySql</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td><input type="text" name="kode_pasien"
placeholder="kode" value="<?php echo $fetched_row['kode_pasien'];
?>" required /></td>
</tr>
<tr>
<td><input type="text" name="nama_pasien"
placeholder="Nama" value="<?php echo $fetched_row['nama_pasien'];
?>" required /></td>
</tr>
<tr>
<td><input type="text" name="jenis_kelamin"
placeholder="Jenis Kelamin" value="<?php echo
$fetched_row['jenis_kelamin']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="gol_darah"
placeholder="Golongan Darah" value="<?php echo
$fetched_row['gol_darah']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="umur" placeholder="Umur"
value="<?php echo $fetched_row['umur']; ?>" required
/></td>
</tr>
<tr>
<td><input type="text" name="alamat" placeholder="Alamat"
value="<?php echo $fetched_row['alamat']; ?>" required
/></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
<button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
</td>
</tr>
</table>
</form>
</div>
</div>
</center>
</body>
</html>
c. Buat dengan nama index.php simpan di file direktori var/www/html/Formulir/
copy
saja script di bawah ini.
<?php
include_once 'dbconfig.php';
// delete condition
if(isset($_GET['delete_id']))
{
$sql_query="DELETE FROM pasien WHERE kode_pasien=".$_GET['delete_id'];
mysql_query($sql_query);
header("Location: $_SERVER[PHP_SELF]");
}
// delete condition
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Rafael Lucky</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript">
function edt_id(id)
{
if(confirm('Sure to edit ?'))
{
window.location.href='edit_data.php?edit_id='+id;
}
}
function delete_id(id)
{
if(confirm('Sure to Delete ?'))
{
window.location.href='index.php?delete_id='+id;
}
}
</script>
<style>
td {
width: 150px;
border: 1px solid crimson;
}
</style>
</head>
<body bgcolor="Skyblue">
<h1><i><center> Tabel Pasien </center></i></h1>
<table align="left" style='border: solid 1px crimson;'>
<th>Kode </th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Golongan Darah</th>
<th>Umur</th>
<th>Alamat</th>
<th colspan="2">Operations</th>
</tr>
<?php
$sql_query="SELECT * FROM pasien";
$result_set=mysql_query($sql_query);
while($row=mysql_fetch_row($result_set))
{
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
<td><?php echo $row[5]; ?></td>
<td align="center"><a href="javascript:edt_id('<?php echo $row[0]; ?>')">Edit</a></td>
<td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')">Delete</a></td>
<td align="center"><a href="insert.php">Add</a></td>
</tr>
<?php
}
?>
</body>
<a href="insert.php"><button>Form Pendaftaran</button></a>
</html>
d. Buat dengan nama insert.php simpan di file direktori var/www
/html/Formulir/ copy
saja script di bawah ini.
<html>
<head>
<style>
body{
background-image: url(images.jpeg);
}
h4 {
background-color:grey;
width: 300px;
padding: 25px;
border: 25px;
margin: 25px;
}
</style>
</head>
<body>
<h1><i><font color="white"><center> SELAMAT
DATANG DI PUSKESMAS BULULAWANG MALANG
</center></font></i></h1>
<center><br>
<h4>
<table>
<th bgcolor="">
<h1>Form Pendaftaran Pasien Baru</h1>
<form action="insert2.php" method="post">
<table>
<tr>
<td>Kode pasien:</td>
<td> <input type="text" name="kode_pasien" /></td>
</tr>
<tr>
<td>Nama Pasien: </td>
<td><input type="text" name="nama_pasien" /></td>
</tr>
<tr>
<td>Jenis Kelamin: </td>
<td><input type="text" name="jenis_kelamin" /></td>
</tr>
<tr>
<td>Golongan Darah:</td>
<td><input type="text" name="gol_darah" /></td>
</tr>
<tr>
<td>Umur: </td>
<td><input type="text" name="umur" /></td>
</tr>
<tr>
<td>Alamat:</td>
<td><input type="text" name="alamat" /></td>
</tr>
<td><input type="submit" name="submit" value="Save" /></td>
<td><a href="index.php" class="Button">Lihat Data</a></td>
</table>
</form>
</th>
</table>
</h4>
</center>
</body>
</html>
e. Buat dengan nama insert2.php simpan di file direktori var/www
/html/Formulir/ copy
saja script di bawah ini.
<html>
<body>
<?php
$con = mysql_connect("localhost","root","thoni");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("puskesmas", $con);
$sql="INSERT INTO pasien (kode_pasien, nama_pasien, jenis_kelamin, gol_darah, umur, alamat)
VALUES
('$_POST[kode_pasien]','$_POST[nama_pasien]','$_POST[jenis_kelamin]','$_POST[gol_darah]','$_POST[umur]','$_POST[alamat]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added".'<br>';
mysql_close($con)
?>
<a href="index.php"><button>Lihat Data</button></a>
</body>
</html>
3. Setelah semuanya sudah di simpan di file direktori var/www/html/ di
dalam folder formulir kemudian mari kita coba dengan cara :
A. Buka web browser anda kemudian tuliskan
localhost/Formulir/insert.php lalu
muncul form seperti gambar di bawah
ini dan silahkan isi form sesuai data
anda contoh sepert gambar di bawah
ini.
Kode Pasien : 1
Nama Pasien : Rudi
Jenis Kelamin : L
Golongan Darah : O
Umur : 18
Alamat : jl.pisang candi
B. Kalau semua sudah di isi lalu klik save dan akan muncul sperti
gambar di
bawah ini lalu klik lihat data untuk melihat data anda sudah
terdaftar dalam
tabel atau belum.
C. Ternyata pendaftaran berhasil
D. Bila anda salah dalam mengisi data anda tidak perlu mengulang
untuk
mendaftar kembali cukup di edit saja contoh seperti gambar di
bawah ini klik
edit.
Tadi saya mengisi data formulir seperti ini yang awalnya.
Kode Pasien : 1
Nama Pasien : Rudi
Jenis Kelamin : L
Golongan Darah : O
Umur : 18
Alamat : jl.pisang candi
Kemudian saya akan mengedit datanya bila sudah di edit klik update dan akan muncul date Are update succesfully lalu klik ok.
Lalu hasilnya seperti gambar ini yang tadinya golongan darah O saya edit
menjadi golongan darah A yang umurnya tadi 18 sekarang menjadi 17 dan
yang tadi alamatnya jl. pisang candi saja sekarang menjadi jl pisang
candi malang.
E. Kemudian saya mencoba tombol Add untuk menambahkan data lagi klik Add
lalu isikan form dengan data baru sesuai kenginan anda.
Masukan data baru contoh seperti gambar di bawah ini kalau sudah klik save.
Kemudian hasilnya akan muncul di tabel seperti gambar di bawah ini
F. Kemudian saya akan mencoba tombol delete yang saya delete
adalah pasien
yang bernama haykal contoh seperti gambar di bawah ini.
Klik delete.
klik Ok.
Kemudian hasilnya data pasien haykal akan terhapus.
Keterangan : Bila anda mencoba dan terjadi error coba lihat pada script
pemanggilan nama database maupun nama tabelnya apakah anda sudah sesuai
dalam mengisi pemanggilan nama database dan nama tabel yang anda buat.
Sekian dulu dari saya semoga anda berhasil bila mencoba tutorial saya di atas.
Wassalamualaikum Wr. Wb.
Jumat, 01 April 2016
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar