Membuat struktur tabel seperti gambar di bawah ini
Lalu membuat struktur folder seperti gambar di bawah ini
(di dalam htdocs)
connection.php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "db_coba";
$connection = mysql_connect($host,$user,$password);
if(!$connection)
{
die("not connect : ".mysql_error());
}
$database = mysql_select_db($database,$connection);
if(!$database)
{
die("database not connect : ".mysql_error());
}
?>
$host = "localhost";
$user = "root";
$password = "";
$database = "db_coba";
$connection = mysql_connect($host,$user,$password);
if(!$connection)
{
die("not connect : ".mysql_error());
}
$database = mysql_select_db($database,$connection);
if(!$database)
{
die("database not connect : ".mysql_error());
}
?>
input.php
<html>
<head>
<title> program crud </title>
<body>
<h3> Program Crud Sederhana </h3>
<form action="prosesinput.php" method="post">
Nama Lengkap :
<input type="text" name="Nama" required="required">
<br>
<br>
Alamat lengkap :
<textarea name="alamat" required>
</textarea>
<br>
<br>
Nomor Telepon :
<input type="number" name="nomortelepon" required="required">
<br>
<br>
<input type="submit" name="input" value="Input">
</form>
</body>
</head>
</html>
<head>
<title> program crud </title>
<body>
<h3> Program Crud Sederhana </h3>
<form action="prosesinput.php" method="post">
Nama Lengkap :
<input type="text" name="Nama" required="required">
<br>
<br>
Alamat lengkap :
<textarea name="alamat" required>
</textarea>
<br>
<br>
Nomor Telepon :
<input type="number" name="nomortelepon" required="required">
<br>
<br>
<input type="submit" name="input" value="Input">
</form>
</body>
</head>
</html>
prosesinput.php
<?php
if(isset($_POST['input']))
{
include "connection.php";
$nama = $_POST['Nama'];
$alamat = $_POST['alamat'];
$nomortelepon = $_POST['nomortelepon'];
if($insert)
{
echo "Input Berhasil di lakukan";
?>
<meta http-equiv ='refresh' content= '1; url=index.php'>
<?php
}
}
?>
if(isset($_POST['input']))
{
include "connection.php";
$nama = $_POST['Nama'];
$alamat = $_POST['alamat'];
$nomortelepon = $_POST['nomortelepon'];
$query = "insert into datacoba (id,Nama,Alamat,No_Telepon) values
('','$nama','$alamat','$nomortelepon')";
$insert = mysql_query($query,$connection) or die("query salah : ".mysql_error());if($insert)
{
echo "Input Berhasil di lakukan";
?>
<meta http-equiv ='refresh' content= '1; url=index.php'>
<?php
}
}
?>
index.php (untuk membaca data)
<?php
include "connection.php";
?>
<html>
<head>
<title> program crud </title>
<body>
<h3> <center> Data Program Crud </center> </h3>
<center>
<table border="5">
<tr>
<td width="2px">No </td>
<td width="200px">Nama Lengkap</td>
<td width="150px">Alamat Lengkap</td>
<td width="125px">Nomor Telepon</td>
<td width="75px">Edit</td>
<td width="75px">Delete</td>
</tr>
<?php
$no = 0;
$query = "select * from datacoba";
$result = mysql_query($query,$connection);
while($row = mysql_fetch_array($result))
{
$no++;
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['Nama']; ?></td>
<td><?php echo $row['Alamat']; ?></td>
<td><?php echo $row['No_Telepon']; ?></td>
<td>
<a href = "edit.php?id=<?php echo $row['id']; ?>">
<button type ="button"> Edit </button> </a>
</td>
<td>
<a href = "hapus.php?id=<?php echo $row['id']; ?>"> <button type ="button"
onclick = "return confirm ('Apakah anda yakin akan delete
<?php echo $row['Nama']; ?> ?')"> Delete </button> </a>
</td>
</tr>
<?php
}
?>
</table>
<br>
<a href="input.php"> <button> Input Data </button> </a>
</center>
</body>
</head>
</html>
include "connection.php";
?>
<html>
<head>
<title> program crud </title>
<body>
<h3> <center> Data Program Crud </center> </h3>
<center>
<table border="5">
<tr>
<td width="2px">No </td>
<td width="200px">Nama Lengkap</td>
<td width="150px">Alamat Lengkap</td>
<td width="125px">Nomor Telepon</td>
<td width="75px">Edit</td>
<td width="75px">Delete</td>
</tr>
<?php
$no = 0;
$query = "select * from datacoba";
$result = mysql_query($query,$connection);
while($row = mysql_fetch_array($result))
{
$no++;
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['Nama']; ?></td>
<td><?php echo $row['Alamat']; ?></td>
<td><?php echo $row['No_Telepon']; ?></td>
<td>
<a href = "edit.php?id=<?php echo $row['id']; ?>">
<button type ="button"> Edit </button> </a>
</td>
<td>
<a href = "hapus.php?id=<?php echo $row['id']; ?>"> <button type ="button"
onclick = "return confirm ('Apakah anda yakin akan delete
<?php echo $row['Nama']; ?> ?')"> Delete </button> </a>
</td>
</tr>
<?php
}
?>
</table>
<br>
<a href="input.php"> <button> Input Data </button> </a>
</center>
</body>
</head>
</html>
edit.php
<?php
if($_GET)
{
include "connection.php";
$id = $_GET['id'];
$query = "select * from datacoba where id='$id'";
$result = mysql_query($query,$connection);
$row = mysql_fetch_array($result);
}
?>
<html>
<head>
<title> program crud </title>
<body>
<h3> Program Crud Sederhana </h3>
<form action="prosesedit.php" method="post">
<input type="hidden" name="id" value ="<?php echo $row['id'];?>">
Nama Lengkap :
<input type="text" name="Nama" value ="<?php echo $row['Nama'];?>" required="required">
<br>
<br>
Alamat lengkap :
<textarea name="alamat" required><?php
echo $row['Alamat'];
?>
</textarea>
<br>
<br>
Nomor Telepon :
<input type="number" name="nomortelepon" value ="<?php echo $row['No_Telepon'];?>" required="required">
<br>
<br>
<input type="submit" name="input" value="Input">
</form>
</body>
</head>
</html>
if($_GET)
{
include "connection.php";
$id = $_GET['id'];
$query = "select * from datacoba where id='$id'";
$result = mysql_query($query,$connection);
$row = mysql_fetch_array($result);
}
?>
<html>
<head>
<title> program crud </title>
<body>
<h3> Program Crud Sederhana </h3>
<form action="prosesedit.php" method="post">
<input type="hidden" name="id" value ="<?php echo $row['id'];?>">
Nama Lengkap :
<input type="text" name="Nama" value ="<?php echo $row['Nama'];?>" required="required">
<br>
<br>
Alamat lengkap :
<textarea name="alamat" required><?php
echo $row['Alamat'];
?>
</textarea>
<br>
<br>
Nomor Telepon :
<input type="number" name="nomortelepon" value ="<?php echo $row['No_Telepon'];?>" required="required">
<br>
<br>
<input type="submit" name="input" value="Input">
</form>
</body>
</head>
</html>
prosesedit.php
<?php
if(isset($_POST['input'])){
include "connection.php";
$Nama = $_POST['Nama'];
$id = $_POST['id'];
$alamat = $_POST['alamat'];
$nomortelepon = $_POST['nomortelepon'];
$query = "UPDATE datacoba SET
Nama='$Nama',
Alamat='$alamat',
No_Telepon='$nomortelepon'
WHERE id = '$id' ";
$result = mysql_query($query,$connection) or die(mysql_error());
if($result){
echo "data sudah di edit";
?>
<meta http-equiv ='refresh' content= '0.3; url=index.php'>
<?php
}
}
?>
if(isset($_POST['input'])){
include "connection.php";
$Nama = $_POST['Nama'];
$id = $_POST['id'];
$alamat = $_POST['alamat'];
$nomortelepon = $_POST['nomortelepon'];
$query = "UPDATE datacoba SET
Nama='$Nama',
Alamat='$alamat',
No_Telepon='$nomortelepon'
WHERE id = '$id' ";
$result = mysql_query($query,$connection) or die(mysql_error());
if($result){
echo "data sudah di edit";
?>
<meta http-equiv ='refresh' content= '0.3; url=index.php'>
<?php
}
}
?>
hapus.php
<?php
if(isset($_GET['id'])){
include "connection.php";
$id = $_GET['id'];
$query = "SELECT * FROM datacoba WHERE id ='$id'";
$result = mysql_query($query,$connection) or die(mysql_error());
if($result)
{
$queryhapus = "DELETE FROM datacoba WHERE id='$id'";
$hapus = mysql_query($queryhapus,$connection);
if($hapus){
echo 'Data user berhasil di hapus! '; //Pesan jika proses hapus berhasil
?>
<meta http-equiv ='refresh' content= '0.3; url=index.php' target="mainFrame">
<?php
}else{
echo 'Gagal menghapus data! ';
}
}
}
?>
if(isset($_GET['id'])){
include "connection.php";
$id = $_GET['id'];
$query = "SELECT * FROM datacoba WHERE id ='$id'";
$result = mysql_query($query,$connection) or die(mysql_error());
if($result)
{
$queryhapus = "DELETE FROM datacoba WHERE id='$id'";
$hapus = mysql_query($queryhapus,$connection);
if($hapus){
echo 'Data user berhasil di hapus! '; //Pesan jika proses hapus berhasil
?>
<meta http-equiv ='refresh' content= '0.3; url=index.php' target="mainFrame">
<?php
}else{
echo 'Gagal menghapus data! ';
}
}
}
?>
Hasil
input.php
edit.php
Hasil Edit
Hasil Delete
Download + Source Code