ribbon iwa

Memanggil ID tertentu untuk di update sesuai data database dengan PHP MySQL

Selamat pagi sobat blogger, kali ini kami akan share bagaimana memanggil id tertentu untuk di update menggunakan PHP.



Dalam sebuah project mengambil id tertentu harus dipahami, jika tidak yang terjadi adalah anda tidak bisa mngupdate data tertentu, tetapi yang terjadi semua data terupdate, inilah pentingnya memanggil ID tertentu pada data database supaya kita bisa mengedit atau update secara satu per satu.

Langsung saja kita langsung memulainya, buat database dengan nama laund kemudina 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 */;
 

Kedua buat file dengan nama config.php file ini berfungsi sebagai konekso database.

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

Berikutnya buat file dengan nama index.php, file ini mempunyai fungsi menampilkan data database.

 <?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://www.tutorialswb.com/">TutorialsWB</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> 

Terakhir 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">&times;</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 } ?> 

Sekian share tutorial singkat di atas, kalian bisa mendownload filenya langsung dibawah untuk mempelajari dan memodifikasinya langsung.


Jika kalian belum paham dengan tutorial di atas silahkan tinggalkan komentar dan mulai berdiskusi dengan kami.


SUBSCRIBE TO OUR NEWSLETTER

close