Belajar PHP Crud dengan Database MySQL

Assalamualaikum Wr. Wb

Hai Kawan J


Saya akan membagikan Bagaimana cara membuat PHP Crud dengan Database My SQL, berikut script dan hasilnya setelah di buka atau di jalankan di browser.




Berikut script dan langkah-langkahnya, buatlah database terlebih dahulu dengan script berikut :


latihan1.sql


CREATE DATABASE latihan1;
USE latihan1;
CREATE TABLE mahasiswa(
 id int(5) PRIMARY KEY AUTO_INCREMENT,
 nama varchar(100) null,
 username varchar(100) null,
 password varchar(100) null,
 email varchar(100)null
);


Setelah itu buatlah script untuk phpnya. Disini ada beberapa script php yaitu config.php, edit.php, hapus.php, index.php, proses.php, tambah.php dan tampil.php

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());
?>

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>Edit Data</h1><br/><br/>

          <form action="" method="post">
                   <table>
                             <tr>
                                      <td>Nama</td>
                                      <td>:</td>
                                      <td><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></td>
                                      <td></td>
                                      <td><input type="submit" name="edit" value="Edit Data" class="btn" /></td>
                             </tr>
                   </table>
          </form>
</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>Latihan 1 -> Crud</title>
            <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header">
            <p> Belajar PHP Crud dengan Database MySQL </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'];
            }
            ?>

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>
<h1><center>Menambahkan Data</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>
                                    <br>
                                                <td></td>
                                                <td></td>
                                                <td><input type="submit" name="tambah" value="Tambah Data" class="btn" /></td>
                                    </tr>
                        </table>
            </form>
</center>
</body>
</html>

tampil.php

<br />
<br />
<br />
<table border="1px">
            <tr>
                        <th>No</th>
                        <th>Nama</th>
                        <th>Username</th>
                        <th>E-mail</th>
                        <th colspan="2">Keterangan</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>
<br></br>
<a href="?page=&action=tambah"><button>Tambah Data</button></a>

Dan berikut script css untuk mempercantik tampilan hasil CRUDnya.


style.css

*{
                        margin: 0;
                        padding: 0;
            }

            #header{
            background-color: #4682B4;
                        position: fixed;
                        height: 50px;
                        text-align:center;
                        width: 100%;

            }

            #header p{
                        color: #ffffff;
                        line-height: 50px;
                        font-size: 35px;
                        color: #00000;
            }
            .isi{
                        clear: both;
                        padding-top: 70px;
                        padding-left: 80px;
                        padding-right: 80px;
                       
            }

            button {
                        padding: 5px;
                        border-radius: 5px;
                        background-color: #00CED1;
            }

            table{
                        border-collapse: collapse;
                        width: 100%;
                        border-radius: 5px;
            }

            th{
                        background-color: #00CED1;
                        padding: 5px;
            }

            .in{
                        padding: 5px;
                        border: 2px #00CED1 solid;

            }

            .btn{
                        padding: 5px;
                        background-color: #00CED1;
                        border-radius: 5px;
                        float: center;
            }
            .center_table{
                        margin-left: 400px;
            }

Sekian yang bisa saya berikan.

Terimakasih sudah membaca postingan ini. Selamat mencoba dan semoga berhasil yaah J

0 komentar: