Вы находитесь на странице: 1из 6

Diktat Praktikum Pemrograman Web II

Praktikum: PHP dan MySQL Database

Studi Kasus: Data Barang

DDL (Data Definition Language)


CREATE DATABASE latihan1;

CREATE TABLE data_barang (


id_barang int(10) auto_increment Primary Key,
kategori varchar(30),
nama varchar(30),
gambar varchar(100),
harga_beli decimal(10,0),
harga_jual decimal(10,0),
stok int(4)
);

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi
Diktat Praktikum Pemrograman Web II

Buat folder php_mysql

file: koneksi.php

1. <?php
2.
3. $host = "localhost";
4. $user = "root";
5. $pass = "";
6. $db = "latihan1";
7.
8. $conn = mysqli_connect($host,$user,$pass,$db);
9.
10. if ($conn == false)
11. {
12. echo "Koneksi ke server gagal.";
13. die();
14. }
15.
16. ?>

file: header.php
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title><?php echo $title;?></title>
5. <link href="style.css" rel="stylesheet"
type="text/css" />
6. </head>
7. <body>
8. <div class="container">
9. <h1>Data Barang</h1>
10. <div class="main">

file: footer.php
1. </div>
2. </div>
3. </body>
4. </html>

file: style.css
1. body {
2. font-family:arial;
3. font-size:14px;
4. line-height:22px;
5. }

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi
Diktat Praktikum Pemrograman Web II

6.
7. .container {
8. width:980px;
9. margin:0 auto;
10. }
11.
12. h1 {
13. border-bottom:1px solid #ddd;
14. margin-bottom:20px;
15. }
16.
17. table {
18. border-collapse:collapse;
19. border-spacing:0;
20. margin:20px 0;
21. }
22.
23. table th,
24. table td {
25. padding: 2px 4px;
26. border:1px solid #ddd;
27. }
28.
29. td img {
30. max-width:75px;
31. max-height:50px;
32. }
33.
34. form {
35. margin:20px 0;
36. }
37.
38. .input, .submit {
39. margin:10px 0;
40. }
41.
42. .input label {
43. display:inline-block;
44. width:150px;
45. }
46.
47. .input select,
48. .input input[type="text"] {
49. border-radius:3px;
50. border:1px solid #ddd;
51. padding: 2px 4px;
52. }
53.

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi
Diktat Praktikum Pemrograman Web II

54. .submit input[type="submit"] {


55. margin-left:150px;
56. border:0;
57. border-radius:5px;
58. background-color:blue;
59. color:#fff;
60. padding: 4px;
61. cursor:pointer;
62. }

file: index.php
1. <?php
2.
3. $title = 'Data Barang';
4. include_once 'koneksi.php';
5.
6. $sql = 'SELECT * FROM data_barang';
7. $result = mysqli_query($conn, $sql);
8.
9. include_once 'header.php';
10.
11. echo '<a href="tambah_barang.php">Tambah Barang</a>';
12. if ($result):
13. ?>
14. <table>
15. <tr>
16. <th>Gambar</th>
17. <th>Nama Barang</th>
18. <th>Katagori</th>
19. <th>Harga Jual</th>
20. <th>Harga Beli</th>
21. <th>Stok</th>
22. <th>Aksi</th>
23. </tr>
24.
25. <?php while($row = mysqli_fetch_array($result)): ?>
26. <tr>
27. <td><?php echo "<img
src=\"{$row['gambar']}\" />";?></td>
28. <td><?php echo $row['nama'];?></td>
29. <td><?php echo $row['kategori'];?></td>
30. <td><?php echo $row['harga_jual'];?></td>
31. <td><?php echo $row['harga_beli'];?></td>
32. <td><?php echo $row['stok'];?></td>
33. <td>
34. <a href="edit_barang.php?id=<?php
echo $row['id_barang'];?>">Edit</a>
35. <a href="hapus_barang.php?id=<?php

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi
Diktat Praktikum Pemrograman Web II

echo $row['id_barang'];?>">Delete</a>
36. </td>
37. </tr>
38. <?php endwhile; ?>
39. </table>
40. <?php
41. endif;
42.
43. include_once 'footer.php';
44. ?>

file: tambah_barang.php
1. <?php
2. error_reporting(E_ALL);
3.
4. $title = 'Data Barang';
5. include_once 'koneksi.php';
6.
7. if (isset($_POST['submit']))
8. {
9. $nama = $_POST['nama'];
10. $kategori = $_POST['kategori'];
11. $harga_jual = $_POST['harga_jual'];
12. $harga_beli = $_POST['harga_beli'];
13. $stok = $_POST['stok'];
14. $file_gambar = $_FILES['file_gambar'];
15. $gambar = null;
16.
17. if ($file_gambar['error'] == 0)
18. {
19. $filename = str_replace(' ', '_',
$file_gambar['name']);
20. $destination = dirname(__FILE__) . '/gambar/' .
$filename;
21. if
(move_uploaded_file($file_gambar['tmp_name'], $destination))
22. {
23. $gambar = 'gambar/' . $filename;;
24. }
25. }
26.
27. $sql = 'INSERT INTO data_barang (nama, kategori,
harga_jual, harga_beli, stok, gambar) ';
28. $sql .= "VALUE ('{$nama}', '{$kategori}',
'{$harga_jual}', '{$harga_beli}', '{$stok}', '{$gambar}')";
29. $result = mysqli_query($conn, $sql);
30.
31. header('location: index.php');

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi
Diktat Praktikum Pemrograman Web II

32. }
33.
34. include_once 'header.php';
35. ?>
36.
37. <h2>Tambah Barang</h2>
38. <form method="post" action="tambah_barang.php"
enctype="multipart/form-data">
39. <div class="input">
40. <label>Nama Barang</label>
41. <input type="text" name="nama" />
42. </div>
43. <div class="input">
44. <label>Kategori</label>
45. <select name="kategori">
46. <option
value="Komputer">Komputer</option>
47. <option
value="Elektronik">Elektronik</option>
48. <option value="Hand Phone">Hand
Phone</option>
49. </select>
50. </div>
51. <div class="input">
52. <label>Harga Jual</label>
53. <input type="text" name="harga_jual" />
54. </div>
55. <div class="input">
56. <label>Harga Beli</label>
57. <input type="text" name="harga_beli" />
58. </div>
59. <div class="input">
60. <label>Stok</label>
61. <input type="text" name="stok" />
62. </div>
63. <div class="input">
64. <label>File Gambar</label>
65. <input type="file" name="file_gambar" />
66. </div>
67. <div class="submit">
68. <input type="submit" name="submit"
value="Simpan" />
69. </div>
70. </form>
71. <?php
72. include_once 'footer.php';
73. ?>

Agung Nugroho (mail@chung.web.id)


STT Pelita Bangsa, Bekasi

Вам также может понравиться