Membuat CRUD Sederhana dengan PHP dan MySqli

7:45:00 AM


CRUD adalah singkatan dari Create, Read, Update, dan Delete yang biasanya untung membangun sebuah aplikasi pengelohan data. Dalam Tutorial Membuat CRUD Sederhana ini menggunakan MySqli (improved).


  • Pertama buatlah Database dengan nama db_mahasiswa


-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 16 Okt 2016 pada 14.46
-- Versi Server: 10.1.13-MariaDB
-- PHP Version: 5.6.23

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `db_mahasiswa`
--

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

--
-- Struktur dari tabel `mhs`
--

CREATE TABLE `mhs` (
  `id` int(11) NOT NULL,
  `nim` varchar(20) NOT NULL,
  `nama` varchar(50) NOT NULL,
  `kelas` varchar(10) NOT NULL,
  `jurusan` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `mhs`
--

INSERT INTO `mhs` (`id`, `nim`, `nama`, `kelas`, `jurusan`) VALUES
(49, '1210003', 'Ihdam Fikri', 'B', 'Sistem Informasi'),
(50, '1210002', 'Fikri', 'B', 'Sistem Informasi');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `mhs`
--
ALTER TABLE `mhs`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `mhs`
--
ALTER TABLE `mhs`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=51;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


  • file koneksi.php

<?php
 
 $host = "localhost";
 $user = "root";
 $pass = "";
 $db   = "db_mahasiswa";
 
    $dbcon = new mysqli ("$host", "$user", "$pass", "$db");
  if($dbcon-> connect_error)
  {
   echo "ERROR -> ".$dbcon ->connect_error;  
  }
 
?>

  • file index.php

<html>
 <head>
  <title> Mahasiswa </title>
 </head>
 
 <body>
  
 <p align = "center">  
  <a href = "index.php"> Home </a> /
  <a href = "tambah.php"> Input Data </a> 
 </p>
 
 <form action = "" method = "POST">
  <table align = "center" cellspacing="0" cellpadding ="5" border="1px" >
   <tr>
    <td>
     ID
    </td>
    <td>
     NIM
    </td>
    <td>
     Nama
    </td>
    <td>
     Kelas
    </td>
    <td>
     Jurusan
    </td>
    <td>
     Options
    </td>
   </tr>
   <?php
    include 'koneksi.php';
    $view = $dbcon -> query("SELECT * FROM mhs");
    while($row=$view->fetch_array())
    {
     ?> 
      <tr>
       
       <td> <?php echo $row['id']; ?></td>
       <td> <?php echo $row['nim']; ?></td>
       <td> <?php echo $row['nama']; ?></td>
       <td> <?php echo $row['kelas']; ?></td>
       <td> <?php echo $row['jurusan']; ?></td>
       
       <td>
        <a href = "edit.php?edit=<?php echo $row['id']; ?>" onClick ="return confirm ('Sure to Edit ?');">Edit </a> ||
        <a href = "delete.php?del=<?php echo $row['id']; ?>" onClick ="return confirm ('Sure to Delete ?');"> Delete </a>
       </td>
       </tr>
     <?php 
    }
   ?>
  </table>
 </form>
 </body>
</html>

  • file tambah.php

<html>
 <head>
  <title>
   Input Data
  </title>
 </head>
 
 <body>
  <h2 align = "center">
   Input Data
  </h2>
  
  <p align = "center">  
   <a href = "index.php"> Home </a> /
   <a href = "tambah.php"> Input Data </a> 
  </p>
 
  
  <form action = "" method = "POST">
   <table align = "center">
    
    <tr>
     <td>
      NIM
     </td>
     <td>
      :
     </td>
     <td>
      <input type = "text" name ="nim" placeholder = "NIM" >
     </td>
    </tr>
    <tr>
     <td>
      Nama
     </td>
     <td>
      :
     </td>
     <td>
      <input type = "text" name ="nama" placeholder = "Nama" >
     </td>
    </tr>
    <tr>
     <td>
      Kelas
     </td>
     <td>
      :
     </td>
     <td>
      <input type = "text" name ="kelas" placeholder = "Kelas" >
     </td>
    </tr>
    <tr>
     <td>
      Jurusan
     </td>
     <td>
      :
     </td>
     <td>
      <input type = "text" name ="jurusan" placeholder = "Jurusan" >
     </td>
    </tr>
    <tr>
     <td>
      
     </td>
     <td>
      
     </td>
     <td>
      <input type = "submit" name ="save" value = "Save" >
     </td>
    </tr>
   </table>
  </form>
  
 </body>
</html>
<?php
  include 'koneksi.php';
  if (isset($_POST['save']))
  {
   
   $nim  = $dbcon -> real_escape_string($_POST['nim']);
   $nama  = $dbcon -> real_escape_string($_POST['nama']);
   $kelas  = $dbcon -> real_escape_string($_POST['kelas']);
   $jurusan = $dbcon -> real_escape_string($_POST['jurusan']);
  
   $SQL = $dbcon -> prepare ("INSERT INTO mhs (id,nim,nama,kelas,jurusan) VALUES (?,?,?,?,?)");
   $SQL -> bind_param("sssss",$id,$nim,$nama,$kelas,$jurusan);
   $SQL -> execute();
   
   if (!$SQL)
   {
    echo $mysqliconn -> error;
   }
   header("Location: index.php");
  }
 ?>

  • file edit.php

<?php
 include 'koneksi.php';
 
 if (isset($_GET['edit']))
 {
  $SQL = $dbcon->query ("SELECT * FROM mhs WHERE id=".$_GET['edit']);
  $getROW = $SQL ->fetch_array();
 }
 if(isset($_POST['update']))
{
  $SQL = $dbcon->prepare("UPDATE mhs SET nim=?, nama=?, kelas=?, jurusan=? WHERE id=?");
  $SQL->bind_param("ssssi",$_POST['nim'],$_POST['nama'],$_POST['kelas'],$_POST['jurusan'],$_GET['edit']);
  $SQL->execute();
  header("Location: index.php");
}
?> 
<html>
<head>
</head>

<body>
 
 <h2 align = "center">
  Edit Data
 </h2>
 <p align = "center">
  <a href = "index.php"> Home </a> /
  <a href = "tambah.php"> Input Data </a> 
 </p>
 <form action ="" method ="POST">
 <table align ="center">
  <tr>
   <td>NIM</td>
   <td>:</td>
   <td><input type="text" name="nim"  value="<?php if(isset($_GET['edit'])) echo $getROW['nim'];  ?>" /></td>
  </tr>
  <tr>
   <td>Nama</td>
   <td>:</td>
   <td><input type="text" name="nama"  value="<?php if(isset($_GET['edit'])) echo $getROW['nama'];  ?>" /></td>
  </tr>
  <tr>
   <td>Kelas</td>
   <td>:</td>
   <td><input type="text" name="kelas"  value="<?php if(isset($_GET['edit'])) echo $getROW['kelas'];  ?>" /></td>
  </tr>
  <tr>
   <td>Jurusan</td>
   <td>:</td>
   <td><input type="text" name="jurusan" value="<?php if(isset($_GET['edit'])) echo $getROW['jurusan'];  ?>" /></td>
  </tr>
  <tr>
   <td></td>
   <td></td>
   <td>
  <?php
   if(isset($_GET['edit']))
   {
  ?>
   <button type="submit" name="update">Update</button>
  <?php
   }
   else
   {
  ?>
  
  <?php
   }
  ?>
   </td> 
  </tr>
 </table>
 </form>
</body>
</html>

  • file delete.php


prepare("DELETE FROM mhs WHERE id=".$_GET['del']);
  $SQL->bind_param("i",$_GET['del']);
  $SQL->execute();
  header("Location: index.php");
 }

?>

You Might Also Like

0 comments