Assalamua'alaikum Wr.Wb
Hai all, kali ini saya akan berbagi postingan Belajar PHP Crud, berikut script dan hasilnya :
Berikut script dan langkah-langkahnya :
1. Langkah pertama buatlah database terlebih dahulu
CREATE DATABASE latihan1;
`id` int(11) NOT NULL,
`nama` varchar(35) NOT NULL,
`username` varchar(35) NOT NULL,
`password` varchar(30) NOT NULL,
`email` varchar(40) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
2. Langkah kedua buat script untuk php. Disini ada beberapa script, yaitu config.php , tambah.php , tampil.php , edit.php , hapus.php , index.php , proses.php .
Lalu copy semua script php dibawah ini :
config.php
<?php
mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('latihan1') or die (mysql_error());
?>
tambah.php
<?php
if(isset($_POST['tambah'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("insert into crud values ('','$nama','$username','$password','$email')");
if ($hasil) {
?>
<script type="text/javascript">
alert("Tambah Data Berhasil");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<h1><center>Tambah Data</center></h1><br/><br/>
<form action="" method="post">
<table class="center_table" >
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan Nama" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan username" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" class="in" placeholder="Masukkan Password" required></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" placeholder="Masukkan E-mail" required></td>
</tr>
<tr>
<td height="50"></td>
<td></td>
<td><input type="submit" name="tambah" value="Tambah Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
tampil.php
<a href="?page=&action=tambah"><center><button>Tambah Data</button></center></a>
<br />
<br />
<table border="1px">
<tr>
<th>No</th>
<th>Nama</th>
<th>Username</th>
<th>E-mail</th>
<th colspan="2">Aksi</th>
</tr>
<?php
include "config.php"; // agar bisa connect ke database
$hasil=mysql_query("select * from crud"); // memilih semua data dari tabel user
$no = 1; //membuat no urut
while ($data=mysql_fetch_array($hasil)) { // pengulangan 'while' agar semua data bisa tampil
?>
<tr align="center">
<td><?php echo $no; ?></td>
<td><?php echo $data['nama'] ?></td>
<td><?php echo $data['username'] ?></td>
<td><?php echo $data['email'] ?></td>
<td><a href="?page=&action=edit&id=<?php echo $data['id'] ?>"><button>Edit</button></a></td>
<td><a href="?page=&action=hapus&id=<?php echo $data['id'] ?>" onclick="return confirm('Apakah Anda ingin menghapus??')"><button>Hapus</button></a></td>
</tr>
<?php
$no++; //agar no bisa urut
} // akhir pengulangan while
?>
</table>
edit.php
<?php
$id = @$_GET['id'];
$sql = mysql_query("select * from crud where id = '$id'") or die(mysql_error()); //memilih data
$data = mysql_fetch_array($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
if(isset($_POST['edit'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("update crud set nama = '$nama', username = '$username', password = '$password', email = '$email' where id = '$id'");
if ($hasil) {
?>
<script type="text/javascript">
alert("Edit Data Berhasil");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}
?>
<h1><center>Edit Data</center></h1><br/><br/>
<form action="" method="post">
<table class="center_table" >
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" value="<?php echo $data['nama']; ?>" /></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" value="<?php echo $data['username']; ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" name="password" class="in" value="<?php echo $data['password']; ?>" /></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" value="<?php echo $data['email']; ?>"/></td>
</tr>
<tr>
<td height="50"></td>
<td></td>
<td><input type="submit" name="edit" value="Edit Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
hapus.php
<?php
$id = @$_GET['id']; // hapus berdasarkan id
mysql_query("delete from crud where id = '$id'") or die (mysql_error()); //query menghapus data
?>
<script type="text/javascript">
window.location.href="?page" // begitu klik hapus otomatis menuju halaman awal
</script>
index.php
<?php
include "config.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>Belajar Crud</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header">
<p> Belajar PHP Crud </p>
</div>
<div class="isi">
<?php
$page = @$_GET['page'];
$action =@$_GET['action'];
if ($page == "") {
if($action == "") {
include "tampil.php";
} else if ($action == "tambah") {
include "tambah.php";
} else if ($action == "edit") {
include "edit.php";
} else if ($action == "hapus") {
include "hapus.php";
}
}
?>
</div>
<div class="fixedBar">
<div class="boxfloat">
</div>
</div>
</body>
</html>
proses.php
<?php
if(isset($_GET['nama']) AND isset($_GET['email]))
{
echo $_GET['nama'];
}
echo "<br/>";
if(isset($_GET['username']))
{
echo $_GET['username'];
}
echo "<br/>";
if(isset($_GET['password']))
{
echo $_GET['password'];
}
echo "<br/>";
if(isset($_GET['email']))
{
echo $_GET['email'];
}
?>
3. Tampilan css untuk mempercantik tampilan, berikut scriptnya :
*{
margin: 0;
padding: 0;
}
#header{
background-color: #9900cc;
position: fixed;
height: 50px;
text-align:center;
width: 100%;
}
#header p{
color: #ffffff;
line-height: 50px;
font-size: 35px;
color: #000000;
}
.isi{
clear: both;
padding-top: 70px;
padding-left: 30px;
padding-right: 30px;
}
button {
padding: 5px;
border-radius: 5px;
background-color: #9933ff;
}
table{
border-collapse: collapse;
width: 100%;
border-radius: 5px;
}
th{
background-color: #9966ff;
padding: 5px;
}
.in{
padding: 5px;
border: 2px #9933ff solid;
}
.btn{
padding: 5px;
background-color: #9966ff;
border-radius: 5px;
}
.center_table{
margin-left: 400px;
}
Sekian postingan dari saya, semoga bermanfaat:)
Hai all, kali ini saya akan berbagi postingan Belajar PHP Crud, berikut script dan hasilnya :
Berikut script dan langkah-langkahnya :
1. Langkah pertama buatlah database terlebih dahulu
CREATE DATABASE latihan1;
`id` int(11) NOT NULL,
`nama` varchar(35) NOT NULL,
`username` varchar(35) NOT NULL,
`password` varchar(30) NOT NULL,
`email` varchar(40) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
2. Langkah kedua buat script untuk php. Disini ada beberapa script, yaitu config.php , tambah.php , tampil.php , edit.php , hapus.php , index.php , proses.php .
Lalu copy semua script php dibawah ini :
config.php
<?php
mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('latihan1') or die (mysql_error());
?>
tambah.php
<?php
if(isset($_POST['tambah'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("insert into crud values ('','$nama','$username','$password','$email')");
if ($hasil) {
?>
<script type="text/javascript">
alert("Tambah Data Berhasil");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<h1><center>Tambah Data</center></h1><br/><br/>
<form action="" method="post">
<table class="center_table" >
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan Nama" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" pattern="[a-zA-Z ]+" placeholder="Masukkan username" required
oninvalid="this.setCustomValidity('Input hanya boleh huruf a-z dan spasi')"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" class="in" placeholder="Masukkan Password" required></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" placeholder="Masukkan E-mail" required></td>
</tr>
<tr>
<td height="50"></td>
<td></td>
<td><input type="submit" name="tambah" value="Tambah Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
tampil.php
<a href="?page=&action=tambah"><center><button>Tambah Data</button></center></a>
<br />
<br />
<table border="1px">
<tr>
<th>No</th>
<th>Nama</th>
<th>Username</th>
<th>E-mail</th>
<th colspan="2">Aksi</th>
</tr>
<?php
include "config.php"; // agar bisa connect ke database
$hasil=mysql_query("select * from crud"); // memilih semua data dari tabel user
$no = 1; //membuat no urut
while ($data=mysql_fetch_array($hasil)) { // pengulangan 'while' agar semua data bisa tampil
?>
<tr align="center">
<td><?php echo $no; ?></td>
<td><?php echo $data['nama'] ?></td>
<td><?php echo $data['username'] ?></td>
<td><?php echo $data['email'] ?></td>
<td><a href="?page=&action=edit&id=<?php echo $data['id'] ?>"><button>Edit</button></a></td>
<td><a href="?page=&action=hapus&id=<?php echo $data['id'] ?>" onclick="return confirm('Apakah Anda ingin menghapus??')"><button>Hapus</button></a></td>
</tr>
<?php
$no++; //agar no bisa urut
} // akhir pengulangan while
?>
</table>
edit.php
<?php
$id = @$_GET['id'];
$sql = mysql_query("select * from crud where id = '$id'") or die(mysql_error()); //memilih data
$data = mysql_fetch_array($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
if(isset($_POST['edit'])){
$nama = @$_POST['nama'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$email = @$_POST['email'];
$hasil=mysql_query("update crud set nama = '$nama', username = '$username', password = '$password', email = '$email' where id = '$id'");
if ($hasil) {
?>
<script type="text/javascript">
alert("Edit Data Berhasil");
window.location.href="?page=";
</script>
<?php
} else {
echo mysql_error();
}
}
?>
<h1><center>Edit Data</center></h1><br/><br/>
<form action="" method="post">
<table class="center_table" >
<tr>
<td width="30">Nama</td>
<td width="20">:</td>
<td width="300"><input type="text" name="nama" class="in" value="<?php echo $data['nama']; ?>" /></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" class="in" value="<?php echo $data['username']; ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" name="password" class="in" value="<?php echo $data['password']; ?>" /></td>
</tr>
<tr>
<td>E-mail</td>
<td>:</td>
<td><input type="text" name="email" class="in" value="<?php echo $data['email']; ?>"/></td>
</tr>
<tr>
<td height="50"></td>
<td></td>
<td><input type="submit" name="edit" value="Edit Data" class="btn" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
hapus.php
<?php
$id = @$_GET['id']; // hapus berdasarkan id
mysql_query("delete from crud where id = '$id'") or die (mysql_error()); //query menghapus data
?>
<script type="text/javascript">
window.location.href="?page" // begitu klik hapus otomatis menuju halaman awal
</script>
index.php
<?php
include "config.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>Belajar Crud</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header">
<p> Belajar PHP Crud </p>
</div>
<div class="isi">
<?php
$page = @$_GET['page'];
$action =@$_GET['action'];
if ($page == "") {
if($action == "") {
include "tampil.php";
} else if ($action == "tambah") {
include "tambah.php";
} else if ($action == "edit") {
include "edit.php";
} else if ($action == "hapus") {
include "hapus.php";
}
}
?>
</div>
<div class="fixedBar">
<div class="boxfloat">
</div>
</div>
</body>
</html>
proses.php
<?php
if(isset($_GET['nama']) AND isset($_GET['email]))
{
echo $_GET['nama'];
}
echo "<br/>";
if(isset($_GET['username']))
{
echo $_GET['username'];
}
echo "<br/>";
if(isset($_GET['password']))
{
echo $_GET['password'];
}
echo "<br/>";
if(isset($_GET['email']))
{
echo $_GET['email'];
}
?>
3. Tampilan css untuk mempercantik tampilan, berikut scriptnya :
*{
margin: 0;
padding: 0;
}
#header{
background-color: #9900cc;
position: fixed;
height: 50px;
text-align:center;
width: 100%;
}
#header p{
color: #ffffff;
line-height: 50px;
font-size: 35px;
color: #000000;
}
.isi{
clear: both;
padding-top: 70px;
padding-left: 30px;
padding-right: 30px;
}
button {
padding: 5px;
border-radius: 5px;
background-color: #9933ff;
}
table{
border-collapse: collapse;
width: 100%;
border-radius: 5px;
}
th{
background-color: #9966ff;
padding: 5px;
}
.in{
padding: 5px;
border: 2px #9933ff solid;
}
.btn{
padding: 5px;
background-color: #9966ff;
border-radius: 5px;
}
.center_table{
margin-left: 400px;
}
Sekian postingan dari saya, semoga bermanfaat:)















