Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $namaServer = "localhost";
- $username = "root";
- $password = "";
- $namaDatabase = "produk_uts";
- //Buat Koneksi
- $koneksi = mysqli_connect($namaServer,$username,$password,$namaDatabase);
- //Cek Koneksi
- if (!$koneksi) {
- die ("Koneksi Gagal: ".mysqli_connect_error());
- }
- if (isset($_POST['submitTambah'])) {
- $id_produk = $_POST['idProduk'];
- $nama_produk = $_POST['namaProduk'];
- $jml_produk = $_POST['jmlProduk'];
- //cek ID Produk
- $cekID = "Select id_produk from produk where id_produk = ".$id_produk;
- $resultCek = mysqli_query($koneksi, $cekID);
- if ($resultCek) {
- echo "ID Produk '".$id_produk."' sudah tersedia";
- }
- else
- {
- // echo "halo";
- $sql = "INSERT INTO produk(id_produk,nama_produk,qty_produk)VALUES('".$id_produk."','".$nama_produk."','".$jml_produk."')";
- $runSql = mysqli_query($koneksi,$sql);
- if ($runSql)
- {
- echo "<br>Produk berhasil ditambah ke dalam database";
- }
- else
- {
- echo "<br> Error : ".$sql."<br>".mysqli_error($koneksi);
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <style type="text/css">
- table, td, tr {
- border : solid 1px black;
- }
- </style>
- <title></title>
- </head>
- <body>
- <form action="#" method="post">
- <div>
- <label>ID Produk : </label>
- <input type="text" name="idProduk">
- </div>
- <div>
- <label>Nama Produk : </label>
- <input type="text" name="namaProduk">
- </div>
- <div>
- <label>Jumlah : </label>
- <input type="text" name="jmlProduk">
- </div>
- <div>
- <input type="submit" name="submitTambah">
- </div>
- </form>
- <hr>
- <div>
- <table>
- <thead>
- <tr>
- <td>ID Produk</td>
- <td>Nama Produk</td>
- <td>Jumlah</td>
- <!-- <td>Action</td> -->
- </tr>
- </thead>
- <tbody>
- <?php
- $selectSql = "Select * from produk";
- $resultSelect = mysqli_query($koneksi, $selectSql);
- $jumlahResultSelect = mysqli_num_rows($resultSelect);
- if ($jumlahResultSelect != 0) {
- while ($row = mysqli_fetch_assoc($resultSelect))
- {
- ?>
- <tr>
- <td><?php echo $row['id_produk']; ?></td>
- <td><?php echo $row['nama_produk']; ?></td>
- <td><?php echo $row['qty_produk']; ?></td>
- <!-- <td><a href="http://localhost/latihan/uts/delete/<?php echo $row['id_produk']; ?>">Delete</a></td> -->
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- <p><a href="home.php">Home</a></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement