Categories
PHP

Memanggil ID tertentu untuk update sesuai database dengan PHP

cara memanggil id tertentu php

Kali ini kami akan mencoba membahas bagaimana memanggil id tertentu pada data database untuk di update menggunakan PHP. Aplikas sederhana seperti ini sangat banyak digunakan karena admin atau pengguna sistem perlu untuk mengupdate suatu data sesuai dengan id yang mereka panggil.

Langsung saja mari kita masuk kedalam pembahasan inti.

1. Buat database dengan nama laund kemudian masukkan struktur database dibawah ini.

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Waktu pembuatan: 19 Nov 2018 pada 22.10
-- Versi server: 10.1.36-MariaDB
-- Versi PHP: 7.2.10

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: `onani`
--

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

--
-- Struktur dari tabel `laundry`
--

CREATE TABLE `laundry` (
  `id_laundry` int(11) NOT NULL,
  `nama_laundry` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `alamat_laundry` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `nohp_laundry` varchar(13) COLLATE utf8_unicode_ci NOT NULL,
  `status` varchar(30) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data untuk tabel `laundry`
--

INSERT INTO `laundry` (`id_laundry`, `nama_laundry`, `alamat_laundry`, `nohp_laundry`, `status`) VALUES
(1, 'ABiDZAR Laundry', 'Jl. Malino, Garassi, Sulawesi-Selatan', '08176757787', 'Verified'),
(2, 'ALIF Laundry', 'Jl. Pettarani, Makassar', '08176757778', 'Verified'),
(3, 'DEDE Laundry', 'Jl. Alauddin, Makassar', '08176757789', 'Block'),
(4, 'MAMAN Laundry', 'Jl. Malengkeri, pasanggarahan, Jakarta', '08175357787', 'Verified');

--
-- Indexes for dumped tables
--

--
-- Indeks untuk tabel `laundry`
--
ALTER TABLE `laundry`
  ADD PRIMARY KEY (`id_laundry`);

--
-- AUTO_INCREMENT untuk tabel yang dibuang
--

--
-- AUTO_INCREMENT untuk tabel `laundry`
--
ALTER TABLE `laundry`
  MODIFY `id_laundry` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=89;
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 */;

2. Buat file dengan nama config.php file ini berfungsi sebagai koneksi kedatabase.

<?php
// conection
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'laund');
$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();
}
?>

3. Buat file dengan nama index.php file berfungsi untuk menampilkan data database kemudian sebagai form utama.

<?php session_start();
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
 <title>GetID</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
 <script src="bootstrap/js/bootstrap.min.js"></script>
 
   <link type="text/css" href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet'>
   <link type="text/css" href='https://cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css' rel='stylesheet'>
   <link type="text/css" href='https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css' rel='stylesheet'>

   <script language="javascript" type="text/javascript">
      var popUpWin=0;
      function popUpWindow(URLStr, center, top, width, height)
      {
         if(popUpWin)
         {
            if(!popUpWin.closed) popUpWin.close();
        }
        popUpWin = open(URLStr,'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+500+',height='+300+',center='+center+', top='+top+',screenX='+center+',screenY='+top+'');
    }

</script>
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://jasawebsite.ooo/">jasawebsite</a>
   </div>
  </div>
 </nav>
 <div class="container">
  <div class="row">
            <br/>
            <table id="example" class="display responsive nowrap" style="width:100%">
              <thead>
                <tr>
                  <th>Id Laundry</th>
                  <th>Nama Laundry</th>
                  <th>Alamat Laundry</th>
                  <th>No Handphone</th>
                  <th>Status</th>
                  <th>Update Status</th>
              </tr>
          </thead>
          <tbody>
            <?php $query=mysqli_query($con, "select * from laundry where id_laundry");
            while($row=mysqli_fetch_array($query)) 
              { ?>  
                <tr>
                  <th scope="row"><?php echo htmlentities($row['id_laundry']);?></th>
                  <td><?php echo htmlentities($row['nama_laundry']);?></td>
                  <td><?php echo htmlentities($row['alamat_laundry']);?></td>
                  <td><?php echo htmlentities($row['nohp_laundry']);?></td>
                  <td class="numeric">
                    <?php $status=$row[ 'status']; if($status=="" or $status=="NULL" ) { ?>
                        <span class="badge badge-secondary">Not Verified</span>
                  <?php } if($status=="Block" ){ ?>
                      <span class="badge badge-secondary">Block</span>
                  <?php } if($status=="Verified" ) { ?>
                      <span class="badge badge-secondary">Verified</span>
                  <?php } ?>
                  <td>
                      <a href="javascript:void(0);" onClick="popUpWindow('update.php?cid=<?php echo htmlentities($row['id_laundry']);?>');" title="Update order">
                       <button type="button" class="btn btn-primary">Update Status</button>
                   </a>
               </td>
           </td>
       </tr>
   <?php  } ?>


</tbody>
</table>
</div>
</div>


<!--Script Javascript-->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js"></script>
<script>
 $(document).ready(function() {
  $('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
    'colvis'
    ]
} );
} );
</script>
</body>
</html>

4. Buat file dengan nama config.php file ini merupakan form pop up dimana anda bisa update status laundry. form ini akan memanggil CID sesuai ID data database yang ingin di update.

<?php session_start();
 include('config.php');
 {
  if(isset($_POST['update']))
  {
    $id_laundry=$_GET['cid'];
    $status=$_POST['status'];
    $query=mysqli_query($con,"insert into laundry(status) values('id_laundry','$status')");
    $sql=mysqli_query($con,"update laundry set status='$status' where id_laundry='$id_laundry'");

    echo "<script>alert('User Status Update Successfull');</script>";

  }

  ?>
  <script src="bootstrap/js/bootstrap.min.js"></script>

    <script language="javascript" type="text/javascript">
    function f2()
    {
      window.close();
    }ser
    function f3()
    {
      window.print(); 
    }
  </script>

  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <script src="js/bootstrap.min.js"></script>
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Update status User</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <form name="updateticket" id="update" method="post" class="form-update">
          <div class="form-group">
            <select class="form-control" name="status">
              <option value="">Select User Status</option>
              <option value="Block">Block</option>
              <option value="Verified">Verified</option>
            </select>
          </div>
          <div class="modal-footer">
            <button type="submit" name="update" class="btn btn-primary">Update</button>
            <button name="Submit2" type="submit" class="btn btn-secondary" value="Close this window " onClick="return f2();">Close</button>
          </div>
        </form>
      </div>

    </div>
  </div>
  <?php } ?>

Kalian bisa mendownload filenya langsung dengan menekan button download dibawah ini.

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda semua, silahkan tinggalkan komentar dan mulai berdiskusi dengan kami.

Leave a Reply

Your email address will not be published. Required fields are marked *