PHP - Update - Edit - Save

Dalam laatihan kali ini ada beberapa file pendukung, agar semuanya bisa berjalan dengan baik.

1. form.php
2. proses.php
3. table.php
4. update.php
5. simpan.php
6. delete.php
7. koneksi.php
8. database.sql

1. form.php

<html>
<head>
<title>FORM PENGISIAN</title>
</head>
<body>
<form method="POST" action="proses.php">
<table border='0'>
<tr>
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" size="20"></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><textarea cols="5" rows="20" name="alamat"></textarea></td>
</tr>
<tr>
<td>Usia</td>
<td>:</td>
<td><input type="text" size="20" name="usia"></td>
</tr>
<tr>
<td><input type="submit" name="proses" value="PROSES"></td>
<td> | </td>
<td><input type="reset" value="BATAL"></td>
</tr>
</table>
</form>
</body>
</html>
________________________________________

2. proses.php

<?php
include "koneksi.php";
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$usia=$_POST['usia'];
$query="insert into biodata(nama,alamat,usia) value('$nama','$alamat','$usia')";
$x=mysql_query($query);
if($x){
echo "Input data anda berhasil<br>";
echo "<a href="tabel.php">Lihat Hasil</a>";
}else{
echo "Gagal Input<br>";
echo "<a href="form.php">KEMBALI</a>";
}
?>
________________________________________

3. table.php

<?php
include "koneksi.php";
$query="select * from biodata";
$x=mysql_query($query);
$n=mysql_num_rows($x);
?>
<table border='1'>
<tr>
<th>Nomor</th>
<th>Nama</th>
<th>Alamat</th>
<th>Usia</th>
<th>Aksi</th>
</tr>
<?php

while($y=mysql_fetch_array($x)){
?>
<tr>
<td><?php echo $c=$c+1;?></td>
<td><?php echo $y['nama']; ?> </td>
<td><?php echo $y['alamat']; ?> </td>
<td><?php echo $y['usia']; ?> </td>
<td><a href="delete.php?id=<?php echo $y['id'];?>" onClick="return confirm('Apakah Anda yakin akan menghapus')" >DELETE</a>
| <a href="update.php?id=<?php echo $y['id'];?>">EDIT</a></td>
</tr>
<?php
}
?>

</table><br>
<a href="form.php">Input Data Form</a>
________________________________________

4. update.php

<?php
include "koneksi.php";
$id=$_GET['id'];
$query=mysql_query("select * from biodata where id='$id'");
while($x=mysql_fetch_array($query)){
?>
<form method="POST" action="simpan.php">
<table border='0'>
<input type="hidden" name="id" value="<?php echo $x['id']; ?>" size="20">
<tr>
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" size="20" value="<?php echo $x['nama']; ?>";></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><textarea cols="5" rows="20" name="alamat"><?php echo $x['alamat']; ?></textarea></td>
</tr>
<tr>
<td>Usia</td>
<td>:</td>
<td><input type="text" size="20" name="usia" value="<?php echo $x['usia']; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="simpan" value="SIMPAN"></td>
</tr>
</table>
</form>
<?php
}
?>
________________________________________

5. simpan.php

<?php
include "koneksi.php";
$id=$_POST['id'];
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$usia=$_POST['usia'];
$query=mysql_query("update biodata set nama='$nama',alamat='$alamat',usia='$usia' where id='$id'");
if($query){
echo "Berhasil Updata Data<br>";
include "tabel.php";
echo "<a href="tabel.php">Lihat Data</a>";
}else{
echo "Gagal Update Data";
}
?>
________________________________________

7. koneksi.php
<?php
$host="localhost";
$user="root";
$pass="";
$db_name="agustus";
$con=mysql_connect($host,$user,$pass);
$db=mysql_select_db($db_name,$con);
?>
________________________________________

8. database.sql
CREATE TABLE IF NOT EXISTS `biodata` (
`id` int(2) NOT NULL AUTO_INCREMENT,
`nama` varchar(20) DEFAULT NULL,
`alamat` varchar(50) DEFAULT NULL,
`usia` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
);

INSERT INTO `biodata` (`id`, `nama`, `alamat`, `usia`) VALUES
(2, 'Haning Distayana', 'Pemalang', 20),
(3, 'Abdu Dhohir', 'Pencongan', 40),
(4, 'Deviana', 'Kajen', 50),
(8, 'abdul Munir', 'bener', 90),
(10, 'yudi santoso', 'solo', 21);

Untuk membuatnya kalian bisa mengimpornya file database.sql ke database. Adapun link downloadnya Disini

Related Posts

Posting Komentar