Tutorial CRUD Dengan PHP + MYSQLi Dan Bootstrap
artikel kali ini tentang membuat Tutorial sederhana CRUD dengan PHP Mysqli dan Bootstrap, Source Code Membuat CRUD dengan PHP MYSQL+Bootstrap ini cocok anda miliki dan anda dapatkan untuk materi pembelajaran anda yang lagi belajar PHP MYSQLi pemula , Belajar dari source Code aplikasi orang akan memerikan warna perbedaan dan pengetahuan yang lebih luas tentang coding, kenapa.. ? karena umumnya karakteristrik pemrograman dasar berbeda-beda.. ok. Langsung saja
CRUD PHP MYSQLi dan Bootstrap
Dalam pemrograman website CRUD (Create,Read,Update,Delete) atau dalam bahasa indonesianya , Membuat Simpan Data, Tampil Data , Update Data serta Hapus Data di PHP MYSQL Bootstrap , ini merupakan hal yang sangat penting , karena ini adalah dasar yang memang harus dikuasi oleh programer website
Beberapa tampilan dari aplikasi CRUD Bootstrap ini dapat anda download disini :
File yang harus di buat :
- index.php
- koneksi.php
- alert.php
- aksi.php
Langkah pertama , Buat lah database dengan nama db_senterwebs.com
kemudian jalan kan query dibawah ini untuk membuat tabel
CREATE TABLE IF NOT EXISTS `t_user` (
`id_user` int(11) NOT NULL,
`nama` varchar(100) NOT NULL,
`alamat` text,
`email` varchar(100) DEFAULT NULL,
`agama` varchar(50) DEFAULT NULL,
`jk` char(1) NOT NULL,
`status` char(1) NOT NULL,
`created_date` datetime NOT NULL,
`modified_date` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=7 ;
Index.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Latihan CRUD Bootstrap PHP MYSQLi</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Author" content="Senterwebs">
<link rel="shortcut icon" href="https://senterwebs.com/favicon.png">
<link rel="icon" href="https://senterwebs.com/favicon.png">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- <link rel="stylesheet" href="bootstrap-3.3.6/dist/css/bootstrap.min.css"> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!-- <script type='javascript' src="bootstrap-3.3.6/dist/js/bootstrap.min.js"></script> -->
</head>
<body>
<div role="navigation" class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://www.Senterwebs.com" class="navbar-brand">SENTERWEBS.COM</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://www.Senterwebs.com">Home</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container">
<?php
include "koneksi.php";
switch(@$_GET['mod']) {
default:
?>
<div class="blog-header text center">
<h2 class="blog-title text-center">CRUD PHP MYSQLi BOOTSTRAP</h2>
<p style='text-align: center;'><a href="https://senterwebs.com/artikel-429/tutorial-crud-dengan-php--mysqli-dan-bootstrap-.html">Back to article senterwebs.com</a></p>
</div>
<p><a href='?mod=add'><button type='button' class='btn btn-primary'><span class='glyphicon glyphicon-plus-sign'></span> Add User</button></a></p>
<div class="row">
<div class="col-md-12">
<?php include"alert.php"; ?>
<table class="table table-striped">
<thead>
<tr>
<th>No.</th>
<th>Nama</th>
<th>Alamat</th>
<th>Email</th>
<th>Agama</th>
<th>J_Kelamin</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$sql = $mysqli->query("SELECT * FROM t_user ");
while ($data = $sql->fetch_array()) {
echo"
<tr>
<td>$no</td>
<td>$data[nama]</td>
<td>$data[alamat]</td>
<td>$data[email]</td>
<td>$data[agama]</td>
<td>$data[jk]</td>
<td>$data[status]</td>
<td><a href='?mod=edit&id=$data[id_user]'><button type='button' class='btn btn-success'><span class='glyphicon glyphicon-edit'></span> Edit</button></a> "; ?>
<a href='aksi.php?mod=delete&id=<?php echo $data['id_user'];?>' onClick="return confirm('Yakin akan menghapus Data?')"><button type='button' class='btn btn-danger'><span class='glyphicon glyphicon-remove-sign'>Delete</button></a></td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
</div>
<?php
break;
case 'add': ?>
<form method='POST' action='aksi.php?mod=tambah' class='form-horizontal'>
<h2>Tambah User</h2>
<div class="form-group">
<label class="col-sm-1 control-label">Nama</label>
<div class="col-sm-4">
<input type="text" name='nama' class="form-control" placeholder="Text input" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Alamat</label>
<div class="col-sm-4">
<textarea class="form-control" name='alamat' rows="3" placeholder="Alamat" required></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Email</label>
<div class="col-sm-4">
<input type="email" name='email' class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Agama</label>
<div class="col-sm-4">
<select class="form-control" name='agama' required>
<option value=''>-Pilih Agama-</option>
<option value='Islam'>Islam</option>
<option value='Kristen'>Kristen</option>
<option value='Katolik'>Katolik</option>
<option value='Hindu'>Hindu</option>
<option value='Budha'>Budha</option>
<option value='Konghuchu'>Konghuchu</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Jenis Kelamin</label>
<div class="col-sm-4">
<input type="radio" name="jk" id="jk" value="L">Laki-laki
<input type="radio" name="jk" id="jk" value="P">Perempuan
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Status</label>
<div class="col-sm-4">
<input type="radio" name="status" id="status" value="A">Aktif
<input type="radio" name="status" id="status" value="T">Tidak Aktif
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
<button type='submit' name='submit' class='btn btn-primary'>Tambah</button>
</div>
</div>
</form>
<?php
break;
case 'edit':
$sql = $mysqli->query("SELECT * FROM t_user WHERE id_user ='$_GET[id]' ");
$data = $sql->fetch_array();
?>
<form method='POST' action='aksi.php?mod=edit' class='form-horizontal'>
<h2>Edit User</h2>
<!-- post kan id user type hidden-->
<input type='hidden' name='id_user' value='<?php echo $data['id_user'];?>'>
<div class="form-group">
<label class="col-sm-1 control-label">Nama</label>
<div class="col-sm-4">
<input type="text" name='nama' class="form-control" placeholder="Nama" value="<?php echo $data['nama'];?> ">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Alamat</label>
<div class="col-sm-4">
<textarea class="form-control" name='alamat' rows="3" placeholder="Alamat"><?php echo $data['alamat'];?></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Email</label>
<div class="col-sm-4">
<input type="email" name='email' class="form-control" placeholder="Email" value="<?php echo $data['email'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Agama</label>
<div class="col-sm-4">
<select class="form-control" name='agama'>
<option value=''>-Pilih Agama-</option>
<option value='Islam' <?php if ($data['agama']=='Islam'){echo"SELECTED";}?>>Islam</option>
<option value='Kristen' <?php if ($data['agama']=='Kristen'){echo"SELECTED";}?>>Kristen</option>
<option value='Katolik' <?php if ($data['agama']=='Katolik'){echo"SELECTED";}?>>Katolik</option>
<option value='Hindu' <?php if ($data['agama']=='Hindu'){echo"SELECTED";}?>>Hindu</option>
<option value='Budha' <?php if ($data['agama']=='Budha'){echo"SELECTED";}?>>Budha</option>
<option value='Konghuchu' <?php if ($data['agama']=='Konghuchu'){echo"SELECTED";}?>>Konghuchu</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Jenis Kelamin</label>
<div class="col-sm-4">
<input type="radio" name="jk" id="jk" value="L" <?php if ($data['jk'] =='L'){ echo "CHECKED"; }?>>Laki-laki
<input type="radio" name="jk" id="jk" value="P" <?php if ($data['jk'] =='P'){ echo "CHECKED"; }?>>Perempuan
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Status</label>
<div class="col-sm-4">
<input type="radio" name="status" id="status" value="A" <?php if ($data['status'] =='A'){ echo "CHECKED"; }?> >Aktif
<input type="radio" name="status" id="status" value="T" <?php if ($data['status'] =='T'){ echo "CHECKED"; }?> >Tidak Aktif
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
<button type='submit' name='submit' class='btn btn-primary' onClick="return confirm('Yakin akan Edit Data?')">Save</button>
<button type='reset' name='reset' class='btn btn-primary'>Reset</button>
</div>
</div>
</form>
<?php
break;
} ?>
</div>
</div>
</body>
</html>
koneksi.php
<?php
$mysqli = new mysqli("localhost", "root", "", "db_senterwebs.com");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
?>
aksi.php
<?php
include "koneksi.php";
$datetime = date("Y-m-d H:i:s");
$id = $mysqli->real_escape_string($_POST['id_user']);
$nama = $mysqli->real_escape_string($_POST['nama']);
$alamat = $mysqli->real_escape_string($_POST['alamat']);
$email = $mysqli->real_escape_string($_POST['email']);
$agama = $mysqli->real_escape_string($_POST['agama']);
$jk = $mysqli->real_escape_string($_POST['jk']);
$status = $mysqli->real_escape_string($_POST['status']);
$id = $_GET['id'];
if ($_GET['mod']=='tambah') {
$sql = $mysqli->query("INSERT INTO t_user (nama, alamat, email, agama, jk, status, created_date) VALUES ('$nama', '$alamat', '$email', '$agama', '$jk', '$status', '$datetime') ");
header('location:index.php?code=1');
}
elseif ($_GET['mod']=='edit') {
$sql = $mysqli->query("UPDATE t_user SET nama='$nama', alamat='$alamat', email='$email', agama='$agama', jk='$jk', status='$status', modified_date='$datetime'
WHERE id_user ='$id' ");
header('location:index.php?code=2');
}
if($_GET['mod']=='delete') {
$sql1 = $mysqli->query("DELETE FROM t_user WHERE id_user ='$id' ");
header('location:index.php?code=3');
}
?>
alert.php
<?php
if (@$_GET['code']=='1') { ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Berhasil!</strong> Data User berhasil di TAMBAH.
</div>
<?php
}
if (@$_GET['code']=='2') { ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Berhasil!</strong> Data User berhasil di EDIT.
</div>
<?php
}
if (@$_GET['code']=='3') { ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Berhasil!</strong> Data User berhasil di DELETE.
</div>
<?php
}
if (@$_GET['code']=='4') { ?>
<div class="alert alert-warning">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error!</strong> Ada error, harap ulangi perintah.
</div>
<?php
}
?>