Cara membuat validasi duplikat email Menggunakan Php MySql

Selamat malam sobat blogger kali ini kami akan share Cara membuat validasi duplikat email Menggunakan Php MySql. Mencegah duplikasi email atau identitas lainnya dalam sebuah aplikasi android atau web sangatlah penting, Coba teman-teman bayangkan jika terdapat duplikasi identitas yang masuk dalam sebua aplikasi, maka yang terjadi adalah admin akan kebingunan mengolah data yang ada.



Yang pertama kalian lakukan adalah membuat file dengan nama
1. config.php
2. index.php
3. check_avalibality.php
4. check_availabilytyiduser.php

yang pertama kita lakukan adalah membuat database, buat databse dengan nama register. Kemudian masukkan databse berikut.


 -- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Aug 10, 2018 at 04:34 PM
-- Server version: 10.1.32-MariaDB
-- PHP Version: 7.2.5

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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: `register`
--

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

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `fullName` varchar(50) DEFAULT NULL,
  `userEmail` varchar(50) DEFAULT NULL,
  `idUser` varchar(20) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `fullName`, `userEmail`, `idUser`, `password`) VALUES
(1, 'arman', 'arman@gmail.com', '1234', 'f69d037742b470f00879e81b7835a1a3'),
(2, 'arman', 'arman@gmail.com', '1234', 'f69d037742b470f00879e81b7835a1a3');

--
-- Indexes for dumped tables
--

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

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;

/*!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 */;
 

Kemudian buat file dengan nama config.php. File ini berfungsi untuk menghubungkan database.

 <?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'register');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?> 

Kemudian buat file dengan nama index.php. Berfungsi sebagai form register.

 
    <?php
include('config.php');
error_reporting(0);
if(isset($_POST['submit']))
{
  $fullname=$_POST['fullname'];
  $email=$_POST['email'];
  $password=md5($_POST['password']);
  $iduser=$_POST['iduser'];
  $query=mysqli_query($con,"insert into users(fullName,userEmail,password,idUser) values('$fullname','$email','$password','$iduser')");
  $msg="Registration successfull. Now You can login !";
}
?>

<!DOCTYPE html>
<html>
<head>
  <title>REGISTER</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  <script>
    function userAvailability() {
      $("#loaderIcon").show();
      jQuery.ajax({
        url: "check_availability.php",
        data: 'email=' + $("#email").val(),
        type: "POST",
        success: function(data) {
          $("#user-availability-status1").html(data);
          $("#loaderIcon").hide();
        },
        error: function() {}
      });
    }
  </script>
  

  <script>
    function iduserAvailability() {
      $("#loaderIcon").show();
      jQuery.ajax({
        url: "check_availabilytyiduser.php",
        data: 'iduser=' + $("#iduser").val(),
        type: "POST",
        success: function(data) {
          $("#iduser-availability-status1").html(data);
          $("#loaderIcon").hide();
        },
        error: function() {}
      });
    }
  </script>
  
  
</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="#">TUTORIALS BAGAIMANA MENCEGAH DUPLIKAT EMAIL MENGGUNAKAN PHP MYADMIN</a>
    </div>
  </div>
</nav>
<br />
<h2 align="center">REGISTER FORM</h2>
<br />
<p style="padding-left:20%; color:green">
  <?php if($msg){ echo htmlentities($msg); }?>
  <form class="login-form" method="post">
   <div class="form-group">
     <label>FULL NAME</label>
     <input class="form-control" name="fullname" type="text" placeholder="Full Name" autofocus>
   </div>
   <div class="form-group">
     <label>EMAIL</label>
     <input class="form-control" type="email" placeholder="Email" id="email" onBlur="userAvailability()" name="email" placeholder="Email" autofocus>
     <span id="user-availability-status1" style="font-size:12px;"></span>
   </div>
   <div class="form-group">
     <label>IDENTITY NUMBER</label>
     <input class="form-control" maxlength="10" name="iduser" type="text" id="iduser" onBlur="iduserAvailability()" placeholder="Identity Number">
     <span id="iduser-availability-status1" style="font-size:12px;"></span>
   </div>
   <div class="form-group">
     <label>PASSWORD</label>
     <input class="form-control" type="password" required="required" name="password" placeholder="Password">
   </div>
   <div class="form-group">
     <button class="btn btn-primary" type="submit" name="submit" id="submit">REGISTER</button>
   </div>
 </form>

</div>
</body>
</html>

 

Kemudian buat file dengan nama check_avalibality.php yang berfungsi sebagai memanggil database user email.

 <?php 
require_once("config.php");
if(!empty($_POST["email"])) {
 $email= $_POST["email"];
 $result =mysqli_query($con,"SELECT userEmail FROM users WHERE userEmail='$email'");
 $count=mysqli_num_rows($result);
 if($count>
  0) { echo "<span style='color:red'>Email already exists .</span>"; echo "
  <script>$('#submit').prop('disabled',true);</script>
  "; } else{ echo "<span style='color:green'>Email available for Registration .</span>"; echo "
  <script>$('#submit').prop('disabled',false);</script>
  ";
 }
}
?> 

Kemudian buat file dengan nama check_availabilytyiduser.php yang berfungsi sebagai memanggil database user email.

 <?php 
require_once("config.php");
if(!empty($_POST["iduser"])) {
 $iduser= $_POST["iduser"];
 $result =mysqli_query($con,"SELECT idUser FROM users WHERE idUser='$iduser'");
 $count=mysqli_num_rows($result);
 if($count>
  0) { echo "<span style='color:red'>iduser already exists .</span>"; echo "
  <script>$('#submit').prop('disabled',true);</script>
  "; } else{ echo "<span style='color:green'>iduser available for Registration .</span>"; echo "
  <script>$('#submit').prop('disabled',false);</script>
  ";
 }
}
?> 

Berikut tampilan dari form yang sudah ketika user belum terdaftar.


Berikut tampilan form ketika user sudah terdaftar di dalam database


Sekian share kali ini jika temna-teman masih belum oaham dengan kodingan di atas silahkan tinggalkan komentar dibawah dan mulai berdiskusi dengan kami. 

Membuat Grafik Highcharts Dengan Php MyAdmin Menggunakan Ajax Bootsrap

Selamat Malam sobat blogger kali ini kami akan share bagaimana membuat Grafik Highcharts Dengan Php MyAdmin. Kami telah banyak membahas cara membuat grafik menggunakan Google chart, tetapi kali ini kami memperluas konten kami dengan membuat grafik menggunakan highcharts.



Read More: 
Cara Menggunakan Google Chart (Line Chart) Pada PHP Part 1
Cara Menggunakan Google Chart (Line Chart) Pada PHP Part 2

Yang pertama kita lakukan adalah membuat databasenya terlebih dahulu. disni kami membuat database dengan nama chart



 -- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 04, 2018 at 04:00 AM
-- Server version: 10.1.32-MariaDB
-- PHP Version: 7.2.5

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


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

--
-- Table structure for table `tblcomplaints`
--

CREATE TABLE `tblcomplaints` (
  `complaintNumber` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `floor` int(11) NOT NULL,
  `room` varchar(50) NOT NULL,
  `facilityName` varchar(50) NOT NULL,
  `buildingName` varchar(50) NOT NULL,
  `typedamage` varchar(50) NOT NULL,
  `complaintDetails` mediumtext NOT NULL,
  `complaintFile` varchar(50) DEFAULT NULL,
  `regDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `remark` mediumtext NOT NULL,
  `techName` varchar(50) NOT NULL,
  `status` varchar(25) DEFAULT NULL,
  `lastUpdationDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tblcomplaints`
--

INSERT INTO `tblcomplaints` (`complaintNumber`, `userId`, `floor`, `room`, `facilityName`, `buildingName`, `typedamage`, `complaintDetails`, `complaintFile`, `regDate`, `remark`, `techName`, `status`, `lastUpdationDate`) VALUES
(1, 1, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'Sementara di proses pak ya', 'admin', 'in process', '2018-05-23 05:24:19'),
(2, 2, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'test', 'admin', 'in process', '2018-05-23 11:30:33'),
(3, 3, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'test', 'admin', 'in process', '2018-05-24 04:47:28'),
(4, 4, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'test ya', 'admin', 'closed', '2018-05-24 14:47:07'),
(5, 5, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'test', 'admin', 'in process', '2018-05-24 14:50:36'),
(6, 6, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', '', 'admin', 'in process', '2018-05-29 09:18:09'),
(7, 7, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', 'sementara di proses pak ya', 'admin', 'in process', '2018-05-23 05:25:17'),
(8, 8, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(9, 9, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(10, 10, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-01-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(11, 1, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', 'admin', 'in process', '2018-05-24 15:30:21'),
(12, 2, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', 'sementara jalan pak ya', 'admin', 'in process', '2018-05-23 05:26:05'),
(13, 3, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', 'admin', 'closed', '2018-05-24 04:48:13'),
(14, 4, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(15, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(16, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(17, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(18, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(19, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(20, 10, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-02-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(21, 1, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(22, 2, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(23, 3, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', 'selesai pak', 'admin', 'closed', '2018-05-24 04:48:46'),
(24, 4, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', 'Di proses pak ya', 'admin', 'in process', '2018-05-24 15:20:39'),
(25, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(26, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(27, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(28, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(29, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(30, 10, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-03-21 04:41:41', 'mantap', 'admin', 'in process', '2018-05-24 15:21:30'),
(31, 1, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(32, 2, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(33, 3, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(34, 4, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(35, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(36, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(37, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', 'Selesai Pak ya', 'admin', 'closed', '2018-05-24 15:15:30'),
(38, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(39, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(40, 10, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(41, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(42, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', 'admin', 'in process', '2018-05-24 15:43:13'),
(43, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(44, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', 'sementara pak', 'admin', 'in process', '2018-05-24 15:14:57'),
(45, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(46, 10, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-04-10 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(47, 1, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(48, 2, 1, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(49, 3, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(50, 4, 2, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(51, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(52, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(53, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', 'admin', 'closed', '2018-05-24 15:30:10'),
(54, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(55, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', 'admin', 'in process', '2018-05-24 15:31:16'),
(56, 20, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(57, 5, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(58, 6, 3, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(59, 7, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(60, 8, 4, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00'),
(61, 9, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', 'admin', 'in process', '2018-05-24 15:29:55'),
(62, 20, 5, 'Engineering and Technolog', 'Platform', 'Campus Building', 'Urgent', 'Test Pak ya', '', '2018-05-20 04:41:41', '', '', NULL, '0000-00-00 00:00:00');


--
-- Indexes for table `tblcomplaints`
--
ALTER TABLE `tblcomplaints`
  ADD PRIMARY KEY (`complaintNumber`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tblcomplaints`
--
ALTER TABLE `tblcomplaints`
  MODIFY `complaintNumber` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=63;
COMMIT; 

Selanjutnya yang harus kalian lakukan adalah memanggil database, tetapi ssebelum memanggil database kita harus terkoneksi dengan daabase yang tadi kalian masukkan, berikut cara koneksinya. Buat file baru dengan nama config.php

 <?php
// conection
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'chart');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?> 

Berikutnya yang mesti anda lakukan adalah membuat formnya, disini kami menggunakan Ajax Bootsrap. berikut tampilan formnya, kemudian beri nama index.php pada form ini kami akan memanggil database di atas berdasarkan bulan.

 <?php session_start();
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
  <title>LINE CHART</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://code.highcharts.com/highcharts.js"></script>
  <script src="https://code.highcharts.com/modules/exporting.js"></script>
  <script src="https://code.highcharts.com/modules/export-data.js"></script>

  
  <?php
  $tahun=date("Y");
  $total = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints where YEAR(regDate)='$tahun'");
  $data = mysqli_fetch_row($total);
  $totalall = $data[0];

  $hasil1 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='01' and YEAR(regDate)='$tahun'");
  $data1 = mysqli_fetch_row($hasil1);
  $jumlah1 = $data1[0];


  $hasil2 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='02' and YEAR(regDate)='$tahun'");
  $data2 = mysqli_fetch_row($hasil2);
  $jumlah2 = $data2[0];


  $hasil3 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='03' and YEAR(regDate)='$tahun'");
  $data3 = mysqli_fetch_row($hasil3);
  $jumlah3 = $data3[0];


  $hasil4 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='04' and YEAR(regDate)='$tahun'");
  $data4 = mysqli_fetch_row($hasil4);
  $jumlah4 = $data4[0];


  $hasil5 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='05' and YEAR(regDate)='$tahun'");
  $data5 = mysqli_fetch_row($hasil5);
  $jumlah5 = $data5[0];


  $hasil6 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='06' and YEAR(regDate)='$tahun'");
  $data6 = mysqli_fetch_row($hasil6);
  $jumlah6 = $data6[0];


  $hasil7 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='07' and YEAR(regDate)='$tahun'");
  $data7 = mysqli_fetch_row($hasil7);
  $jumlah7 = $data7[0];


  $hasil8 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='08' and YEAR(regDate)='$tahun'");
  $data8 = mysqli_fetch_row($hasil8);
  $jumlah8 = $data8[0];


  $hasil9 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='09' and YEAR(regDate)='$tahun'");
  $data9 = mysqli_fetch_row($hasil9);
  $jumlah9 = $data9[0];


  $hasil10 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='10' and YEAR(regDate)='$tahun'");
  $data10 = mysqli_fetch_row($hasil10);
  $jumlah10 = $data10[0];

  $hasil11 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='11' and YEAR(regDate)='$tahun'");
  $data11 = mysqli_fetch_row($hasil11);
  $jumlah11 = $data11[0];


  $hasil12 = mysqli_query($con,"SELECT count(*) as jum FROM tblcomplaints WHERE MONTH(regDate)='12' and YEAR(regDate)='$tahun'");
  $data12 = mysqli_fetch_row($hasil12);
  $jumlah12 = $data12[0];

  ?>


</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="#">GOOGLE CHART</a>
    </div>
    <ul class="nav navbar-nav navbar-right">
      <li class="dropdown">
       <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="label label-pill label-danger count" style="border-radius:10px;"></span> <span class="glyphicon glyphicon-envelope" style="font-size:18px;"></span></a>
       <ul class="dropdown-menu"></ul>
     </li>
   </ul>
 </div>
</nav>
<br />
<h2 align="center">LINE CHART</h2>
<br />
<form method="post">
 <div class="form-group">
   <div id="container"></div>
 </div>
</form>

</div>


<script>
  Highcharts.chart('container', {

    chart: {
      type: 'column'
    },

    title: {
      text: 'The Complaint Chart'
    },
    subtitle: {
      text: 'Total Complaint, Not Process, In Process, and Closed Complaint, Periode: <?php $tgl=date('Y'); echo $tgl; ?>'
    },

    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']
    },


    tooltip: {
     pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
     shared: true
   },

   plotOptions: {
    column: {
      stacking: 'normal'
    }
  },

  series: [{
    name: 'Total Complaint',
    data: [<?php echo $jumlah1 = $data1[0]; ?>, <?php echo $jumlah2 = $data2[0]; ?>, <?php echo $jumlah3 = $data3[0]; ?>, <?php echo $jumlah4 = $data4[0]; ?>, <?php echo $jumlah5 = $data5[0]; ?>, <?php echo $jumlah6 = $data6[0]; ?>, <?php echo $jumlah7 = $data7[0]; ?>, <?php echo $jumlah8 = $data8[0]; ?>, <?php echo $jumlah9 = $data9[0]; ?>, <?php echo $jumlah10 = $data10[0]; ?>, <?php echo $jumlah11 = $data11[0]; ?>, <?php echo $jumlah12 = $data12[0]; ?>],
  },]
});
</script>

</body>
</html> 

Kami sengaja langsung membuat form dan menghubungkannya kedabase langsung karena kita ingin melihat partisipasi anda untuk berkomentar di bawah. Sekian share kali ini semoga apa yang kami share bermanfaat bagi kalian semua. 

Form Login dan Register Menggunakan Session PHP PDO

Kembal lagi bersama kami, kali ini kami akan share bagaimana Form Login  dan Register Menggunakan Session PHP PDO. Pada potingan sebelumnya kami telah membahas beberapa tutorial Php.



Yang pertama yang mesti kita lakukan adalah membuat databasenya terlebih dahulu. Buat databse dengan nama tblusers

 -- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 29, 2018 at 07:00 PM
-- Server version: 10.1.32-MariaDB
-- PHP Version: 7.2.5

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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: `tblusers`
--

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

--
-- Table structure for table `tblusers`
--

CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `FullName` varchar(120) DEFAULT NULL,
  `EmailId` varchar(100) DEFAULT NULL,
  `Password` varchar(100) DEFAULT NULL,
  `ContactNo` char(11) DEFAULT NULL,
  `RegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tblusers`
--

INSERT INTO `tblusers` (`id`, `FullName`, `EmailId`, `Password`, `ContactNo`, `RegDate`) VALUES
(1, 'arman', 'arman@gmail.com', '21232f297a57a5a743894a0e4a801fc3', '081524737', '2018-07-29 16:46:27'),
(2, 'iwa', 'abi@gmail.com', '21232f297a57a5a743894a0e4a801fc3', '12345', '2018-07-29 16:59:52');

--
-- Indexes for dumped tables
--

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

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tblusers`
--
ALTER TABLE `tblusers`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;

/*!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 */;
 

Yang kedua adalah membuat koneksi kedatabase, disini kami membuat file tersendiri untuk koneksi ke database dengan nama config.php Tujuannya adalah kita lebih muda membuat koneksi kefile lain dengan hanya memanggil file config.php

 <?php 
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','tblusers');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?> 

Yang ketiga adalah membuat form register sekaligus kita hubungkan langsung kedabasenya. Buat file dengan nama register.php

 <?php
//error_reporting(0);
session_start();
include('config.php');
error_reporting(0);
if(isset($_POST['signup']))
{
  $fname=$_POST['fullname'];
  $email=$_POST['emailid']; 
  $mobile=$_POST['mobileno'];
  $password=md5($_POST['password']); 
  $sql="INSERT INTO  tblusers(FullName,EmailId,ContactNo,Password) VALUES(:fname,:email,:mobile,:password)";
  $query = $dbh->prepare($sql);
  $query->bindParam(':fname',$fname,PDO::PARAM_STR);
  $query->bindParam(':email',$email,PDO::PARAM_STR);
  $query->bindParam(':mobile',$mobile,PDO::PARAM_STR);
  $query->bindParam(':password',$password,PDO::PARAM_STR);
  $query->execute();
  $lastInsertId = $dbh->lastInsertId();
  if($lastInsertId)
  {
    echo "<script>alert('Registration successfull. Now you can login');</script>";
  }
  else 
  {
    echo "<script>alert('Something went wrong. Please try again');</script>";
  }
}
?>

<!DOCTYPE html>
<html>
<head>
  <title>Login Register PDO</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="register.php">REGISTER</a>
      <a class="navbar-brand" href="index.php">LOGIN</a>
    </div>
  </div>
</nav>
<br />
<h2 align="center">Please Register</h2>
<br/>
<form method="post" name="signup" onSubmit="return valid();">
 <div class="form-group">
   <label>FULNAME</label>
   <input type="text" class="form-control" name="fullname" placeholder="Full Name*" required="required">
 </div>
 <div class="form-group">
   <label>MOBILE NO</label>
   <input type="text" class="form-control" name="mobileno" placeholder="Mobile Number*" maxlength="10" required="required">
 </div>
 <div class="form-group">
   <label>EMAIL</label>
   <input type="email" class="form-control" name="emailid" id="emailid" placeholder="Email Address*" required="required">
 </div>
 <div class="form-group">
   <label>ENTER PASSWORD</label>
   <input type="password" class="form-control" name="password" placeholder="Password*" required="required">
 </div>
 <div class="form-group">
   <label>CONFIRM PASSWORD</label>
   <input type="password" class="form-control" name="confirmpassword" placeholder="Confirm Password*" required="required">
 </div>
 <div class="form-group">
   <button class="btn btn-primary" type="submit" name="signup" id="submit">REGISTER</button>
 </div>
</form>
</div>
</body>
</html>
 

Yang keempat yang perlu kita buat adalah membuat form login, buat file baru dengan nama index.php

 <?php
session_start();
include('config.php');
error_reporting(0);
if(isset($_POST['login']))
{
  $email=$_POST['email'];
  $password=md5($_POST['password']);
  $sql ="SELECT EmailId,Password,FullName FROM tblusers WHERE EmailId=:email and Password=:password";
  $query= $dbh -> prepare($sql);
  $query-> bindParam(':email', $email, PDO::PARAM_STR);
  $query-> bindParam(':password', $password, PDO::PARAM_STR);
  $query-> execute();
  $results=$query->fetchAll(PDO::FETCH_OBJ);
  if($query->rowCount() > 0)
  {
    $_SESSION['login']=$_POST['email'];
    $_SESSION['fname']=$results->FullName;
    $currentpage=$_SERVER['REQUEST_URI'];
    echo "<script type='text/javascript'> document.location = 'dashbord.php'; </script>";
  } else{
    
    echo "<script>alert('Invalid Details');</script>";

  }
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Login Register PDO</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="register.php">REGISTER</a>
      <a class="navbar-brand" href="index.php">LOGIN</a>
    </div>
  </div>
</nav>
<br />
<h2 align="center">Please Login</h2>
<br />
<form method="post">
 <div class="form-group">
   <label>ENTER EMAIL</label>
   <input type="email" class="form-control" name="email" placeholder="Email address*">
 </div>
 <div class="form-group">
   <label>ENTER PASSWORD</label>
   <input type="password" class="form-control" name="password" placeholder="Password*">
 </div>
 <div class="form-group">
   <button class="btn btn-primary" type="submit" name="login">LOGIN</button>
 </div>
</form>
</div>
</body>
</html>
 

Yang kelima kita membuat form dashbord, buat file baru dengan nama dashbord.php

 <!DOCTYPE html>
<html>
<head>
  <title>Login Register PDO</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <br /><br />
  <div class="container">
   <nav class="navbar navbar-inverse">
    <div class="container-fluid">
     <div class="navbar-header">
      <a class="navbar-brand" href="dashbord.php">HOME</a>
      <a class="navbar-brand" href="logout.php">LOGOUT</a>
    </div>
  </div>
</nav>
<br />
<h2 align="center">WELCOME TO TUTORIALS SIMPLE LOGIN AND LOGOUT USING PHP PDO</h2>
<br />
</div>
</body>
</html>
 

Yang terakhir kita buat file baru dengan nama logout.php, bertujuan untuk mengeksekusi proses logout.

 <?php
session_start(); 
$_SESSION = array();
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 60*60,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}
unset($_SESSION['login']);
session_destroy(); // destroy session
header("location:index.php"); 
?>

 
Jika kalian ingin melihat langsung hasilnya silahkan klik button demo dibawah ini. Kalian bisa mencoba sendiri.


Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, Kami juga tidak menjelaskan secara detail satu per satu kodingan di atas, karena kami sangat menginginkan partisipasi anda untuk menanyakan sesuatu hal yang kurang anda mengerti di kolom komentar.

Operasi CRUD PHP menggunakan Extensi PDO

Selamat siang sobat blogger kali ini kami akan share bagaimana membuat operasi crud PHP emnggunakna extensi PDO. Sebelum itu teman-teman harus mengetahui terlebih dahulu, apa itu PDO,


"PDO (PHP Data Objects) adalah interface universal yang disediakan PHP untuk “berkomunikasi” dengan database server. Maksud istilah “interface universal” disini adalah bahwa PDO tidak terikat dengan aplikasi database tertentu. Apabila saat ini kita menggunakan database MySQL dan dikemudian hari ingin bermigrasi menggunakan PostgreSQL, kita hanya tinggal mengganti cara pemanggilan awal PDO dan seluruh kode program yang ada bisa langsung digunakan untuk database baru."

Step 1 - kita buat adalah membuat databasenya, Berikut databasenya dengan nama 'user'
 -- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Jul 24, 2018 at 11:37 AM
-- Server version: 10.0.35-MariaDB
-- PHP Version: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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: `armg3295_user`
--

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

--
-- Table structure for table `tblusers`
--

CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `FirstName` varchar(150) NOT NULL,
  `LastName` varchar(150) NOT NULL,
  `EmailId` varchar(120) NOT NULL,
  `ContactNumber` char(11) NOT NULL,
  `Address` varchar(255) NOT NULL,
  `PostingDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tblusers`
--

INSERT INTO `tblusers` (`id`, `FirstName`, `LastName`, `EmailId`, `ContactNumber`, `Address`, `PostingDate`) VALUES
(1, 'Abi', 'iwa', 'arman@student.ta.ac.id', '123', 'Malino92', '2018-07-24 04:35:04');

--
-- Indexes for dumped tables
--

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

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tblusers`
--
ALTER TABLE `tblusers`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!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 */;
 

Step 2 - kalian harus membuat koneksi kedatabase, buat file dengan nama dbconfig.php

 <?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','user');
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS);
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?> 

Step 3 - Buat file dengan nama insert.php berfungsi sebagai form untuk menginput data kedalam database

 <?php
// Memanggil file koneksi kedabase
require_once'dbconfig.php';
if(isset($_POST['insert']))
{

// Value posting 
  $fname=$_POST['firstname'];
  $lname=$_POST['lastname'];
  $emailid=$_POST['emailid'];
  $contactno=$_POST['contactno'];
  $address=$_POST['address'];

// Quenry untuk insert
  $sql="INSERT INTO tblusers(FirstName,LastName,EmailId,ContactNumber,Address) VALUES(:fn,:ln,:eml,:cno,:adrss)";

//Permintaan untuk eksekusi
  $query = $dbh->prepare($sql);

// Ikat parameternya
  $query->bindParam(':fn',$fname,PDO::PARAM_STR);
  $query->bindParam(':ln',$lname,PDO::PARAM_STR);
  $query->bindParam(':eml',$emailid,PDO::PARAM_STR);
  $query->bindParam(':cno',$contactno,PDO::PARAM_STR);
  $query->bindParam(':adrss',$address,PDO::PARAM_STR);
// Query Eksekusi
  $query->execute();

// Periksa apakah pemasangan benar-benar berfungsi. Jika ID yang dimasukkan terakhir lebih besar dari nol, penyisipannya berfungsi.
  $lastInsertId = $dbh->lastInsertId();
  if($lastInsertId)
  {
// Pesan jika inser data sukses
    echo "<script>alert('Record inserted successfully');</script>";
    echo "<script>window.location.href='index.php'</script>"; 
  }
  else 
  {
// Pesan jika insert data tidak berhasil
    echo "<script>alert('Something went wrong. Please try again');</script>";
    echo "<script>window.location.href='index.php'</script>"; 
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Chart</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>
  <br/>
  <div class="container">
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
       <div class="navbar-header">
        <a class="navbar-brand" href="#">Operasi CRUD PHP menggunakan Extensi PDO</a>
      </div>
    </div>
  </nav>

  <form name="insertrecord" method="post">
    <div class="row">
      <div class="col-md-4"><b>First Name</b>
        <input type="text" name="firstname" class="form-control" required>
      </div>
      <div class="col-md-4"><b>Last Name</b>
        <input type="text" name="lastname" class="form-control" required>
      </div>
    </div>

    <div class="row">
      <div class="col-md-4"><b>Email id</b>
        <input type="email" name="emailid" class="form-control" required>
      </div>
      <div class="col-md-4"><b>Contactno</b>
        <input type="text" name="contactno" class="form-control" maxlength="10" required>
      </div>
    </div>  

    <div class="row">
      <div class="col-md-8"><b>Address</b>
        <textarea class="form-control" name="address" required></textarea>
      </div>
    </div>  

    <div class="row" style="margin-top:1%">
      <div class="col-md-8">
        <input type="submit" name="insert" value="Submit">
      </div>
    </div>         
  </form>

</div>
</div>
</body>
</html> 

Step 4- Buat file dengan nama index.php yang berfungsi sebagai form untuk memanggil/menempatkan form yang ada di database.

 <?php
// Memanggil file koneksi kedabase
require_once'dbconfig.php';

// Code untuk record Deleted
if(isset($_REQUEST['del']))
{
//Memanggil id baris
  $uid=intval($_GET['del']);

//Query untuk menghapus
  $sql = "delete from tblusers WHERE  id=:id";

// Persiapan query untuk eksekusi
  $query = $dbh->prepare($sql);

// Ikat Parameter
  $query-> bindParam(':id',$uid, PDO::PARAM_STR);

// Query eksekusi
  $query -> execute();

// Pesan sesudah updae
  echo "<script>alert('Record Updated successfully');</script>";

// Kode untuk redirect
  echo "<script>window.location.href='index.php'</script>"; 
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Chart</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>
  <br/>
  <div class="container">
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
       <div class="navbar-header">
        <a class="navbar-brand" href="#">Operasi CRUD PHP menggunakan Extensi PDO</a>
      </div>
    </div>
  </nav>
  <a href="insert.php"><button class="btn btn-primary"> Insert Record</button></a>
  <div class="table-responsive">                
    <table id="mytable" class="table table-bordred table-striped">                 
      <thead>
        <th>No</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Contact</th>
        <th>Address</th>
        <th>Posting Date</th>
        <th>Edit</th>
        <th>Delete</th>
      </thead>
      <tbody>
        
        <?php 
        $sql = "SELECT FirstName,LastName,EmailId,ContactNumber,Address,PostingDate,id from tblusers";
//Persiapan untuk Query
        $query = $dbh->prepare($sql);
// Jalankan Query
        $query->execute();
// Tetapkan data yang Anda tarik dari database (pada langkah sebelumnya) ke variabel.
        $results=$query->fetchAll(PDO::FETCH_OBJ);
// Untuk serial number initalization
        $cnt=1;
        if($query->rowCount() > 0)
        {
//Jika kueri mengembalikan paling tidak satu rekaman, ini dapat mengulangi rekaman dalam loop foreach:
          foreach($results as $result)
          {               
            ?>  
            <tr>
              <td><?php echo htmlentities($cnt);?></td>
              <td><?php echo htmlentities($result->FirstName);?></td>
              <td><?php echo htmlentities($result->LastName);?></td>
              <td><?php echo htmlentities($result->EmailId);?></td>
              <td><?php echo htmlentities($result->ContactNumber);?></td>
              <td><?php echo htmlentities($result->Address);?></td>
              <td><?php echo htmlentities($result->PostingDate);?></td>

              <td><a href="update.php?id=<?php echo htmlentities($result->id);?>"><button class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-pencil"></span></button></a></td>

              <td><a href="index.php?del=<?php echo htmlentities($result->id);?>"><button class="btn btn-danger btn-xs" onClick="return confirm('Do you really want to delete');"><span class="glyphicon glyphicon-trash"></span></button></a></td>
            </tr>
            

            <?php 
// Untuk nomor auto increment
            $cnt++;
          }} ?>
        </tbody>      
      </table>
    </div>
  </div>
</body>
</html> 

Step 5- Buat file dengan nama update.php berfungsi sebagai form untuk update data yang di tampilkan dari database.

 <?php
require_once'dbconfig.php';
if(isset($_POST['update']))
{

  $userid=intval($_GET['id']); 
  $fname=$_POST['firstname'];
  $lname=$_POST['lastname'];
  $emailid=$_POST['emailid'];
  $contactno=$_POST['contactno'];
  $address=$_POST['address'];

  $sql="update tblusers set FirstName=:fn,LastName=:ln,EmailId=:eml,ContactNumber=:cno,Address=:adrss where id=:uid";
  $query = $dbh->prepare($sql);
  $query->bindParam(':fn',$fname,PDO::PARAM_STR);
  $query->bindParam(':ln',$lname,PDO::PARAM_STR);
  $query->bindParam(':eml',$emailid,PDO::PARAM_STR);
  $query->bindParam(':cno',$contactno,PDO::PARAM_STR);
  $query->bindParam(':adrss',$address,PDO::PARAM_STR);
  $query->bindParam(':uid',$userid,PDO::PARAM_STR);

  $query->execute();
  echo "<script>alert('Record Updated successfully');</script>";
  echo "<script>window.location.href='index.php'</script>"; 
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Chart</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>
  <br/>
  <div class="container">
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
       <div class="navbar-header">
        <a class="navbar-brand" href="#">Operasi CRUD PHP menggunakan Extensi PDO</a>
      </div>
    </div>
  </nav>

  <?php 
  $userid=intval($_GET['id']);
  $sql = "SELECT FirstName,LastName,EmailId,ContactNumber,Address,PostingDate,id from tblusers where id=:uid";
  $query = $dbh->prepare($sql);
  $query->bindParam(':uid',$userid,PDO::PARAM_STR);
  $query->execute();
  $results=$query->fetchAll(PDO::FETCH_OBJ);
  $cnt=1;
  if($query->rowCount() > 0)
  {
    foreach($results as $result)
    {               
      ?>
      <form name="insertrecord" method="post">
        <div class="row">
          <div class="col-md-4"><b>First Name</b>
            <input type="text" name="firstname" value="<?php echo htmlentities($result->FirstName);?>" class="form-control" required>
          </div>
          <div class="col-md-4"><b>Last Name</b>
            <input type="text" name="lastname" value="<?php echo htmlentities($result->LastName);?>" class="form-control" required>
          </div>
        </div>

        <div class="row">
          <div class="col-md-4"><b>Email id</b>
            <input type="email" name="emailid" value="<?php echo htmlentities($result->EmailId);?>" class="form-control" required>
          </div>
          <div class="col-md-4"><b>Contactno</b>
            <input type="text" name="contactno" value="<?php echo htmlentities($result->ContactNumber);?>" class="form-control" maxlength="10" required>
          </div>
        </div>  

        <div class="row">
          <div class="col-md-8"><b>Address</b>
            <textarea class="form-control" name="address" required><?php echo htmlentities($result->Address);?></textarea>
          </div>
        </div>  
      <?php }} ?>

      <div class="row" style="margin-top:1%">
        <div class="col-md-8">
          <input type="submit" name="update" value="Update">
        </div>
      </div> 
    </form>
  </div>
</div>

</body>
</htm 

Jika teman-teman ingin melihat demony alangsung silkan klik button demo dibawah ini.


Sekian share kali ini semoga apa yang kami shar bermanfaat bagi anda semua, jangan lupa share dan tinggalkan komentar jika temna-teman belum paham, dan kami menginginkan anda meninggalkan komentar apa saja yang mestikami posting di lain waktu.