Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. <?php
  2. $namaServer = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $namaDatabase = "produk_uts";
  6.  
  7. //Buat Koneksi
  8. $koneksi = mysqli_connect($namaServer,$username,$password,$namaDatabase);
  9.  
  10. //Cek Koneksi
  11. if (!$koneksi) {
  12. die ("Koneksi Gagal: ".mysqli_connect_error());
  13. }
  14.  
  15. if (isset($_POST['submitTambah'])) {
  16. $id_produk = $_POST['idProduk'];
  17. $nama_produk = $_POST['namaProduk'];
  18. $jml_produk = $_POST['jmlProduk'];
  19.  
  20. //cek ID Produk
  21. $cekID = "Select id_produk from produk where id_produk = ".$id_produk;
  22. $resultCek = mysqli_query($koneksi, $cekID);
  23.  
  24. if ($resultCek) {
  25. echo "ID Produk '".$id_produk."' sudah tersedia";
  26. }
  27. else
  28. {
  29. // echo "halo";
  30. $sql = "INSERT INTO produk(id_produk,nama_produk,qty_produk)VALUES('".$id_produk."','".$nama_produk."','".$jml_produk."')";
  31. $runSql = mysqli_query($koneksi,$sql);
  32.  
  33. if ($runSql)
  34. {
  35. echo "<br>Produk berhasil ditambah ke dalam database";
  36. }
  37. else
  38. {
  39. echo "<br> Error : ".$sql."<br>".mysqli_error($koneksi);
  40. }
  41. }
  42. }
  43. ?>
  44. <!DOCTYPE html>
  45. <html>
  46. <head>
  47. <style type="text/css">
  48. table, td, tr {
  49. border : solid 1px black;
  50. }
  51. </style>
  52.  
  53. <title></title>
  54. </head>
  55. <body>
  56. <form action="#" method="post">
  57. <div>
  58. <label>ID Produk : </label>
  59. <input type="text" name="idProduk">
  60. </div>
  61. <div>
  62. <label>Nama Produk : </label>
  63. <input type="text" name="namaProduk">
  64. </div>
  65. <div>
  66. <label>Jumlah : </label>
  67. <input type="text" name="jmlProduk">
  68. </div>
  69. <div>
  70. <input type="submit" name="submitTambah">
  71. </div>
  72. </form>
  73. <hr>
  74. <div>
  75. <table>
  76. <thead>
  77. <tr>
  78. <td>ID Produk</td>
  79. <td>Nama Produk</td>
  80. <td>Jumlah</td>
  81. <!-- <td>Action</td> -->
  82. </tr>
  83. </thead>
  84. <tbody>
  85. <?php
  86. $selectSql = "Select * from produk";
  87. $resultSelect = mysqli_query($koneksi, $selectSql);
  88. $jumlahResultSelect = mysqli_num_rows($resultSelect);
  89.  
  90. if ($jumlahResultSelect != 0) {
  91. while ($row = mysqli_fetch_assoc($resultSelect))
  92. {
  93. ?>
  94. <tr>
  95. <td><?php echo $row['id_produk']; ?></td>
  96. <td><?php echo $row['nama_produk']; ?></td>
  97. <td><?php echo $row['qty_produk']; ?></td>
  98. <!-- <td><a href="http://localhost/latihan/uts/delete/<?php echo $row['id_produk']; ?>">Delete</a></td> -->
  99. </tr>
  100. <?php
  101. }
  102. }
  103. ?>
  104. </tbody>
  105. </table>
  106. </div>
  107. <p><a href="home.php">Home</a></p>
  108. </body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement