Membuat aplikasi dasar PHP dengan Operasi CRUD

 Index.php

<center>

<h2>Website pengisian data</h2>

</center>

    <hr>

      <!-- CSS only -->

<link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<!-- JavaScript Bundle with Popper -->

<script src="js/bootstrap.bundle.js"></script>

<form method="POST">

<!-- <button name="image1" value="btnimg1">Gambar 1</button> -->

            

<a href="index.php?page=require" class="btn btn-info">Tampil Data</a>

<div class="konten bg-light" style="margin-top: 4%; height: 70vh;">

            <?php

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

        $page = $_GET['page'];

        switch ($page) {

            case 'require':

                include "require.php";

                break;

            case 'tambah':

                include "tambah.php";

                break;


            default:

                echo "<h1 style='padding: 20%; color: red;'>😫 Maaf halaman yang anda tuju tidak ada</h1>";

                break;

        }

    }

    ?>

    </div>

</form>

<center>

<?php include_once 'footer.php';?>

</center>

--------------------------------------------------------------------------------footer.php

<?php 

echo "<hr>";

echo "<p>Web Ini dibuat oleh Trisna Almuti</p>";

?>

-----------------------------------------------------------------------------------

require.php

 <!DOCTYPE html>

 <html>

 <head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- CSS only -->

<link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<!-- JavaScript Bundle with Popper -->

<script src="js/bootstrap.bundle.js"></script>

  <title>PHP 7</title>

 </head>

 <body>

  <div class="m-5 p-5" style="overflow-x:auto;">

    <h1>Hasil Data</h1>

 <table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center">

<thead>

<th>Nama</th>

</thead>

<?php 

 include 'koneksi.php';

$hasil = mysqli_query($koneksi, "SELECT * FROM tb_data");

$angka = 1;

foreach ($hasil as $data ) {

?>

<tr>

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

</tr>

<?php } ?>

 </table>

  <a href="index.php?page=tambah" class="btn btn-primary">TAMBAH DATA</a>

  </div>

 </body>

 </html>

-----------------------------------------------------------------------------------

tambah.php

    <?php 


require 'koneksi.php';


if (isset($_POST['submit'])) {

if (tambah($_POST) > 0) {

echo "

<script>

alert('Data Berhasil Ditambahkan');

document.location.href = 'index.php?page=require';

</script>

";

} else {

echo "

<script>

alert('Data Gagal Ditambahkan');

document.location.href = 'index.php?page=tambah';

</script>

";

}

}

 ?>


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- CSS only -->

  <link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<!-- JavaScript Bundle with Popper -->

<script src="js/bootstrap.bundle.js"></script>

<title>PHP 7</title>

</head>

<body>

    <div class="m-5 p-5">

<form method="POST">

<label for="nama">Masukkan Nama : </label>

<input type="text" name="nama" required>

<br>

    <hr>

<input type="submit" name="submit" value="KIRIM!" class="btn btn-primary">

<input type="reset" name="reset" value="RESET" class="btn btn-warning">

</form>

</div>

</body>

</html>

-----------------------------------------------------------------------------------

koneksi.php

<?php  


$koneksi = mysqli_connect("localhost", "root", "", "php7");


function tambah($data){

global $koneksi;


$nama = $_POST['nama'];


$query = mysqli_query($koneksi, "INSERT INTO tb_data (nama) VALUES ('$nama')");


return mysqli_affected_rows($koneksi);

}


?>

-----------------------------------------------------------------------------------

Hasil :


Database :


----------------------------------------------------------------------------------------------------

2. Radio Button

index.php

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>PHP 3</title>

</head>

<body>

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

        <label> nama : </label>

        <input type="text" name="nama"><br><br>

        <label> kelas : </label>

        <input type="radio" value="Pria" name="jeniskel">Pria

        <input type="radio" value="Wanita" name="jeniskel">Wanita

        <!-- <select name="kelas">

        <option value="X">X</option>

        <option value="XI">XI</option>

        <option value="XII">XII</option> -->

        </select><br><br>

        <input type="submit" value="kirim" name="proses">

        <input type="reset" value="reset">

</form>

</body>

</html>

----------------------------------------------------------------------------------------------------

koneksi.php

<?php

$conn = mysqli_connect("localhost", "root", "", "praktek3");

?>

----------------------------------------------------------------------------------------------------

tampilan.php

<?php


include "koneksi.php";


if (isset($_POST['proses'])) {

    mysqli_query($conn, "insert into data_3 set

    NAMA = '$_POST[nama]',

    jeniskel = '$_POST[jeniskel]'");

    echo "*Data baru telah tersimpan";

}

?>


<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<h1> TAMPILKAN DATA </h1>

<table border="1">

<tr>

<th>ID</th>

<th>NAMA</th>

                <th>Jenis Kelamin</th>

            </tr>


<?php

include 'koneksi.php';

$tampil = mysqli_query($conn, "SELECT * FROM data_3 order by ID desc limit 1");

foreach ($tampil as $data) {

    ?>

 <tr>

  <td><?php echo $data["id"]; ?></td>

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

                <td><?php echo $data["jeniskel"]; ?></td>

</tr>

<?php

}


?>


</table>

</body>

</html>

----------------------------------------------------------------------------------------------------

Hasil :


----------------------------------------------------------------------------------------------------

3. Combo Box

index.php

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/bootstrap.css">

    <script src="js/bootstrap.bundle.js"></script>

    <title>Praktek 2</title>

</head>

<body>

    <div class="m-5 w-10 vh-100 align-content-center justify-content-center d-flex top-50 bg-dark text-bg-dark" >

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

        <label> Nama : </label>

        <input type="text" name="nama" class="input-group-text"><br><br>

        <label> kelas : </label>

        <!-- <input type="radio" value="Pria" name="jeniskel">Pria

        <input type="radio" value="Wanita" name="jeniskel">Wanita -->

        <select name="kelas" class="btn btn-primary">

        <option value="X">X</option>

        <option value="XI">XI</option>

        <option value="XII">XII</option>

        </select><br><br>

        <input type="submit" value="kirim" name="proses" class="btn btn-success">

        <input type="reset" value="reset" class="btn btn-warning">

        <a href="" class="btn btn-info">Tampil Data</a>

        </div>

</form>

</body>

</html>

----------------------------------------------------------------------------------------------------

koneksi.php

<?php

$conn = mysqli_connect("localhost", "root", "", "praktek2");

?>

----------------------------------------------------------------------------------------------------

tampilan.php

<?php


include "koneksi.php";


if (isset($_POST['proses'])) {

mysqli_query($conn, "insert into data_2 set

NAMA = '$_POST[nama]',

kelas = '$_POST[kelas]'");

echo "<div class='bg-success text-white p-3'><h6>Data berhasil disimpan</h6></div>";

}

?>


<!DOCTYPE html>

<html>

<link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/bootstrap.css">

    <script src="js/bootstrap.bundle.js"></script>

<head>

<title></title>

</head>

<body>

<div style="overflow-x:auto;" class="m-5 rounded">

<h1> TAMPILKAN DATA </h1>

<table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center rounded">

<tr>

<th>ID</th>

<th>NAMA</th>

<th>Kelas</th>

</tr>


<?php

include 'koneksi.php';

$tampil = mysqli_query($conn, "SELECT * FROM data_2");

foreach ($tampil as $data) {

?>

<tr>

<td><?php echo $data["id"]; ?></td>

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

<td><?php echo $data["kelas"]; ?></td>

</tr>

<?php

}


?>


</table>

<a href="Index.php" class="btn btn-primary">Tambah Data</a>

</div>

</body>

</html>

----------------------------------------------------------------------------------------------------

Hasil :

 ----------------------------------------------------------------------------------------------------

4. Check Box

index.php

<?php 

require 'koneksi.php';

if (isset($_POST['submit'])) {

if (tambah($_POST)>0){

echo "<h6 class='p-3 bg-success text-bg-success'>Proses Input Data Berhasil</h6>";

header("location: tampilan.php");

}else{

echo "<h6 class='p-3 bg-danger text-bg-danger'>Proses Input Data Gagal</h6>";

header("location: index.php");

}

}

 ?>

 <!DOCTYPE html>

 <html>

 <head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

 <link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script src="js/bootstrap.bundle.js"></script>

  <title>Praktek 4</title>

 </head>

 <body>

 <div class="m-5 p-5 bg-dark w-25 text-bg-dark rounded">

  <form method="POST">

  <label>Nama : </label>

  <input type="text" name="nama" class="input-group-text" required>

  <br>

  <label>Hobi : </label>

 <br>

  <input type="checkbox" name="hoby[]" value="Olahraga">Olahraga

  <input type="checkbox" name="hoby[]" value="Gamers">Gamers

  <input type="checkbox" name="hoby[]" value="SLEBEW">SLEBEW

  <br><br>

  <input type="submit" name="submit" class="btn btn-primary fw-bold">

  <input type="reset" name="reset" class="btn btn-warning fw-bold">

<br> <br>

<a href="tampilan.php" class="btn btn-danger fw-bold">KEMBALI</a>

  </form>

 </div>

 </body>

 </html>

<!-- <div class="m-5 p-5 bg-dark w-25 text-bg-dark">

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

        <label> Nama : </label>

        <input type="text" name="nama" class="input-group-text" required><br>

        <label for="hoby"> Hoby : </label> <br>

        <input type="checkbox" name="hoby[]" value="Olahraga" class="">Olahraga

<input type="checkbox" name="hoby[]" value="Gamers">Gamers

<input type="checkbox" name="hoby[]" value="Slebew">Slebew

        <br><br>

        <input type="submit" value="KIRIM" name="proses" class="btn btn-primary">

        <input type="reset" value="RESET" class="btn btn-warning">

</form>

</div> -->

----------------------------------------------------------------------------------------------------

koneksi.php

<?php 

$koneksi = mysqli_connect("localhost","root","","praktek4") ;

function tambah($data){

global $koneksi;

$nama = $_POST['nama'];

$hobi = $_POST['hoby'];

$value = implode(",", $hobi);

$query_mysql = mysqli_query($koneksi, "INSERT INTO data_3 (nama,hoby) VALUES ('$nama','$value')");

return mysqli_affected_rows($koneksi);

}

?>

----------------------------------------------------------------------------------------------------

tampilan.php

<?php 

require 'koneksi.php';

$sql = mysqli_query($koneksi, "SELECT * FROM data_3") ;

 ?>

 <!DOCTYPE html>

 <html>

 <head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="css/bootstrap.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script src="js/bootstrap.bundle.js"></script>

  <title>Praktek 4</title>

 </head>

 <body>

 <div style="overflow-x:auto;" class="m-5">

 <h1> TAMPILKAN DATA </h1>

   <table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center">

<tr>   

<th>ID</th>

<th>Nama</th>

<th>Hobi</th>

</tr>

<?php 

foreach ($sql as $row) {

?>

<tr>

<td><?php echo $row ["id"]; ?></td>

<td><?php echo $row ["nama"]; ?></td>

<td><?php echo $row ["hoby"]; ?></td>

</tr>

<?php } ?>

   </table>

   <a href="Index.php" class="btn btn-primary">Tambah Data</a>

   </div>

 </body>

 </html>

<!-- <div style="overflow-x:auto;" class="m-5">

<h1> TAMPILKAN DATA </h1>

<table class="table table-dark table-hover table-striped-columns text-center justify-content-center align-content-center">

<thead>

<th>ID</th>

<th>Nama</th>

<th>Hobi</th>

</thead>

<tbody>

</tbody>

</table>

<a href="Index.php" class="btn btn-primary">Tambah Data</a>

</div> -->

----------------------------------------------------------------------------------------------------

Hasil :





Posting Komentar

Lebih baru Lebih lama