Selasa, 19 Juli 2022

Membuat CRUD dengan php dan my sql

 CRUD adalah singkatan dari Create ,Read,Update dan Delete .jadi di singkat dengan crud, crud disini yang mengelola database .misalnya seperti menginput data  ke database (create) .menampilkan data dari database (read) .mengubah atau mengupdate data pada database (update) ,dan menghapus data pada database (delete).

Cara menampilkan data dari database dengan php

Pertama kita buat dulu sebuah file php dengan nama terserah teman- teman.langkah selanjutnya kita akan membuat file koneksi.php  di file koneksi.php inilah nanti nya kita buat koneksi php dan my sql .

Koneksi.php

<?php 

$db ="haniayunda";

$host = mysqli_connect("localhost", "root","","haniayunda");

?>


Untuk edit edit.php


<!DOCTYPE html>

<html>

<head>

<title>Membuat CRUD Dengan PHP Dan MySQL - Menampilkan data dari database</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

<div class="judul">

<h1>Membuat CRUD Dengan PHP Dan MySQL</h1>

<h2>Menampilkan data dari database</h2>

<h3>www.haniayunda.com</h3>

</div>

<br/>

<a href="index.php">Lihat Semua Data</a>

 

<br/>

<h3>Edit data</h3>

 

<?php 

include "koneksi.php";

$id = $_GET['id'];

$query_mysql = mysqli_query($host, "SELECT * FROM user WHERE id='$id'")or die(mysql_error());

$nomor = 1;

while($data = mysqli_fetch_array($query_mysql)){

?>

<form action="update.php" method="post">

<table>

<tr>

<td>Nama</td>

<td>

<input type="hidden" name="id" value="<?php echo $data['1'] ?>">

<input type="text" name="nama" value="<?php echo $data['hani'] ?>">

</td>

</tr>

<tr>

<td>Alamat</td>

<td><input type="text" name="alamat" value="<?php echo $data['sakti lubis'] ?>"></td>

</tr>

<tr>

<td>Pekerjaan</td>

<td><input type="text" name="pekerjaan" value="<?php echo $data['barista'] ?>"></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Simpan"></td>

</tr>


<tr>

<td>Nama</td>

<td>

<input type="hidden" name="id" value="<?php echo $data['2'] ?>">

<input type="text" name="nama" value="<?php echo $data['ayunda'] ?>">

</td>

</tr>

<tr>

<td>Alamat</td>

<td><input type="text" name="alamat" value="<?php echo $data['menteng'] ?>"></td>

</tr>

<tr>

<td>Pekerjaan</td>

<td><input type="text" name="pekerjaan" value="<?php echo $data['barista'] ?>"></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Simpan"></td>

</tr>

</table>

</form>

<?php } ?>

</body>

</html>

Untuk hapus hapus.php


<?php 

include 'koneksi.php';

$id = $_GET['1'];

mysqli_query($host, "DELETE FROM user WHERE id='$id'")or die(mysql_error());

 

include 'koneksi.php';

$id = $_GET['2'];

mysqli_query($host, "DELETE FROM user WHERE id='$id'")or die(mysql_error());

 

header("location:index.php?pesan=hapus");

?>


< html>

<head>

<title>Membuat CRUD Dengan PHP Dan MySQL - Menampilkan data dari database</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

<div class="judul">

<h1>Membuat CRUD Dengan PHP Dan MySQL</h1>

<h2>Menampilkan data dari database</h2>

<h3>www.haniayunda.com</h3>

</div>

<br/>

 

<a href="index.php">Lihat Semua Data</a>

 

<br/>

<h3>Input data baru</h3>

<form action="input-aksi.php" method="post">

<table>

<tr>

<td>Nama</td>

<td><input type="text" name="hani"></td>

</tr>

<tr>

<td>Alamat</td>

<td><input type="text" name="sakti lubis"></td>

</tr>

<tr>

<td>Pekerjaan</td>

<td><input type="text" name="barista"></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Simpan"></td>

</tr>

<tr>

<td>Nama</td>

<td><input type="text" name="ayunda"></td>

</tr>

<tr>

<td>Alamat</td>

<td><input type="text" name="menteng"></td>

</tr>

<tr>

<td>Pekerjaan</td>

<td><input type="text" name="barista"></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Simpan"></td>

</tr>

</table>

</form>

</body>

</html>

Untuk input aksi input-aksi.php


<?php 

include 'koneksi.php';

$nama = $_POST['hani'];

$alamat = $_POST['sakti lubis'];

$pekerjaan = $_POST['barista'];

 

mysqli_query($host, "INSERT INTO user VALUES('1','$salsa','$Tanjung morawa','$Web programer')");

 

include 'koneksi.php';

$nama = $_POST['ayunda'];

$alamat = $_POST['menteng'];

$pekerjaan = $_POST['barista']


mysqli_query($host, "INSERT INTO user VALUES('2','$nurhalijah','$Tembung','$Web programer')");

header("location:index.php?pesan=input");

?>


Silahkan teman-teman sesuaikan Username dan password mysql teman-teman.kemudian buat sebuah file index.php ini kita akan menampilkan data dari database mysql.tempatnya sama di tempat file yang telah kita buat.

Index.php


<!DOCTYPE html>

<html>

<head>

<title>Membuat CRUD Dengan PHP Dan MySQL - Menampilkan data dari database</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

<div class="judul">

<h1>Membuat CRUD Dengan PHP Dan MySQL</h1>

<h2>Menampilkan data dari database</h2>

<h3>www.haniayunda.com</h3>

</div>

<br/>

 

<?php 

if(isset($_GET['pesan'])){

$pesan = $_GET['pesan'];

if($pesan == "input"){

echo "Data berhasil di input.";

}else if($pesan == "update"){

echo "Data berhasil di update.";

}else if($pesan == "hapus"){

echo "Data berhasil di hapus.";

}

}

?>

<br/>

<a class="tombol" href="input.php">+ Tambah Data Baru</a>

 

<h3>Data user</h3>

<table border="1" class="table">

<tr>

<th>No</th>

<th>Nama</th>

<th>Alamat</th>

<th>Pekerjaan</th>

<th>Opsi</th>

</tr>

<?php 

include "koneksi.php";

$query_mysql = mysqli_query($host,"SELECT * FROM user")or die(mysql_error());

$nomor = 1;

while($data = mysqli_fetch_array($query_mysql)){

?>

<tr>

<td><?php echo $nomor++; ?></td>

<td><?php echo $data['nama']; ?></td>

<td><?php echo $data['alamat']; ?></td>

<td><?php echo $data['pekerjaan']; ?></td>

<td>

<a class="edit" href="edit.php?id=<?php echo $data['id']; ?>">Edit</a> |

<a class="hapus" href="hapus.php?id=<?php echo $data['id']; ?>">Hapus</a>

</td>

</tr>

<?php } ?>

</table>

</body>

</html>

Dan jangan lupa kita percantik tampilan nya agar enak di pandang mata.

Style.css


body{
font-family: 'roboto';
color: #000;
}
 
.judul{
background: #87D1D8;
padding: 10px;
text-align: center;
 
}
 
.judul h1,h2,h3{
height: 15px;
}
 
a{
/*color: #fff;*/
padding: 5px;
text-decoration: none;
}
 
 
.table{
border-collapse: collapse;
}
 
table.table th th , table.table tr td{
padding: 10px 20px ;
}



Langsung saja kita jalankan file index.php untuk melihat hasil menampilkan data dari database dengan php.tampilan nya akan seperti ini











Tidak ada komentar:

Posting Komentar

Cara mengubah isi tabel database menggunakan command promt

 Untuk mengubah isi tabel database ,anda harus membuat tabel database nya terlebih dahulu.baca  https://hanozkan.blogspot.com/2022/07/belaja...