---
# System prepended metadata

title: Untitled

---

## Soal No Ke 4

Buatlah script untuk menampilkan form inputan dan melakukan insert data ke dalam tabel database (table_barang). Script PHP yang dibuat harus mampu menjalankan fungsi untuk Create, Read, Update, Delete data beserta tampilan form dan tabel(output) HTML nya: [Bobot: 30] 

### Insert & Result Data

```php=
<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_npm1234";

$connection = new mysqli($servername, $username, $password, $database);

if ($connection->connect_error) {
echo die("Connection Failed: " . $connection->connect_error);
}

// initialize all function method
insertData($connection);

function insertData($conn) {

 if(isset($_POST["submit"])) {
    $kode_barang = $_POST["kode_barang"];
    $nama_barang = $_POST["nama_barang"];
    $harga_barang = $_POST["harga_barang"];
    $jumlah_barang = $_POST["jumlah_barang"];

    $query = "INSERT INTO table_barang(kode_barang, nama_barang, harga_barang, jumlah_barang)
    VALUES('$kode_barang','$nama_barang','$harga_barang','$jumlah_barang')";

    if($conn->query($query) === TRUE) {
      alert("Insert new record successfully");
    } else {
      alert("Insert new record failed");
    }
  }
}

function resultData($conn) {
  $query = "SELECT * FROM table_barang";
  $data = $conn->query($query);

 if($data->num_rows > 0) {
    return $data;
  }
}

function alert($msg) {
  echo "<script>alert('$msg');</script>";
}

function redirect($path) {
  echo $path;
}
?>
```
```htmlembedded=
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
</head>

<body>
<div class="container mt-3">
  <div class="row d-flex flex-fill justify-content-center">
    <div class="col-6">
      <div class="card">
        <div class="card-header bg-white text-center">
          <h4>Insert New Record</h4>
        </div>
        <div class="card-body">
          <form autocomplete="off" method="POST">
            <div class="form-group">
              <label for="kode_barang" class="font-weight-bold">Kode Barang</label>
              <input type="text" name="kode_barang" class="form-control" placeholder="Masukan Kode Barang"
                required />
            </div>
            <div class="form-group">
              <label for="nama_barang" class="font-weight-bold">Nama Barang</label>
              <input type="text" name="nama_barang" class="form-control" placeholder="Masukan Nama Barang"
                required />
            </div>
            <div class="form-group">
              <label for="harga_barang" class="font-weight-bold">Harga Barang</label>
              <input type="number" name="harga_barang" class="form-control"
                placeholder="Masukan Harga Barang" required />
            </div>
            <div class="form-group">
              <label for="jumlah_barang" class="font-weight-bold">Jumlah Barang</label>
              <input type="number" name="jumlah_barang" class="form-control"
                placeholder="Masukan Jumlah Barang" required />
            </div>
            <div class="form-group">
              <input type="submit" name="submit" class="form-control btn btn-primary"
                value="Add Record" />
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="container mt-5">
  <div class="row">
    <div class="col-12">
      <div class="card">
        <div class="card-header text-center bg-white">
          <h4>Record List</h4>
        </div>
        <div class="card-body">
          <table class="table table-bordered table-hover table-stripped text-center">
            <thead>
              <tr>
                <th>No</th>
                <th>Kode Barang</th>
                <th>Nama Barang</th>
                <th>Harga Barang</th>
                <th>Jumlah Barang</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              <?php $id = 1; ?>
              <?php foreach(resultData($connection) as $value) { ?>
              <tr>
                <td> <?= $id++; ?></td>
                <td> <?= $value["kode_barang"] ?> </td>
                <td> <?= $value["nama_barang"] ?> </td>
                <td> <?= $value["harga_barang"] ?> </td>
                <td> <?= $value["jumlah_barang"] ?> </td>
                <td>
                  <a href="delete.php?del=<?= $value["id"] ?>"
                    class="btn btn-danger text-decoration-none">
                    Delete </a>
                  <a href="edit.php?edit=<?= $value["id"] ?>"
                    class="btn btn-success text-decoration-none">
                    Edit </a>
                </td>
              </tr>
              <?php }?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

</body>

</html>
```

### Delete Data

```php=
<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_npm1234";

$connection = new mysqli($servername, $username, $password, $database);

if ($connection->connect_error) {
  echo die("Connection Failed: " . $connection->connect_error);
}

// initialize all function method
deleteData($_GET["del"], $connection);

function deleteData($id, $conn) {
  $query = "DELETE FROM table_barang WHERE id = '$id' ";

  if($conn->query($query) === TRUE) {
    alert("Delete new record successfully");
    redirect("/app");
  } else {
    alert("Delete new record failed");
    redirect("/app");
  }
}

function alert($msg) {
  echo "<script>alert('$msg');</script>";
}

function redirect($path) {
  echo "<script>window.location.href = '$path';</script>";
}
?>
```

### Edit & Update Data

```php=
<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_npm1234";

$connection = new mysqli($servername, $username, $password, $database);

if ($connection->connect_error) {
  echo die("Connection Failed: " . $connection->connect_error);
}

// initialize data
updateData($connection);

function editData($id, $conn) {
  $query = "SELECT * FROM table_barang WHERE id = '$id' ";
  $data = $conn->query($query);
  return $data;
}

function updateData($conn) {

  if(isset($_POST["submit"])) {
    $id = $_POST["id"];
    $kode_barang = $_POST["kode_barang"];
    $nama_barang = $_POST["nama_barang"];
    $harga_barang = $_POST["harga_barang"];
    $jumlah_barang = $_POST["jumlah_barang"];

    $query = "UPDATE table_barang
    SET kode_barang = '$kode_barang', nama_barang = '$nama_barang', harga_barang = '$harga_barang', jumlah_barang = '$jumlah_barang' WHERE id = '$id' ";

    $data = $conn->query($query);

    if($data === TRUE) {
      alert("Update new record successfully");
      redirect("/app");
    } else {
      alert("Update new record failed");
      redirect("/app");
    }
  }
}

function alert($msg) {
  echo "<script>alert('$msg');</script>";
}

function redirect($path) {
  echo "<script>window.location.href = '$path';</script>";
}

?>
```

```htmlembedded=
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
  </head>

  <body>
    <div class="container mt-3">
      <div class="row d-flex flex-fill justify-content-center">
        <div class="col-6">
          <div class="card">
            <div class="card-header bg-white text-center">
              <h4>Update New Record</h4>
            </div>
            <div class="card-body">
              <form autocomplete="off" method="POST">
                <?php foreach(editData($_GET["edit"], $connection) as $value) { ?>
                <div class="form-group">
                  <input type="text" name="id" class="form-control" value="<?= $value["id"] ?>" hidden />
                </div>
                <div class="form-group">
                  <label for="kode_barang" class="font-weight-bold">Kode Barang</label>
                  <input type="text" name="kode_barang" class="form-control" placeholder="Masukan Kode Barang"
                    value="<?= $value["kode_barang"] ?>" required />
                </div>
                <div class="form-group">
                  <label for="nama_barang" class="font-weight-bold">Nama Barang</label>
                  <input type="text" name="nama_barang" class="form-control" placeholder="Masukan Nama Barang"
                    value="<?= $value["nama_barang"] ?>" required />
                </div>
                <div class="form-group">
                  <label for="harga_barang" class="font-weight-bold">Harga Barang</label>
                  <input type="number" name="harga_barang" class="form-control"
                    placeholder="Masukan Harga Barang" value="<?= $value["harga_barang"] ?>" required />
                </div>
                <div class="form-group">
                  <label for="jumlah_barang" class="font-weight-bold">Jumlah Barang</label>
                  <input type="number" name="jumlah_barang" class="form-control"
                    placeholder="Masukan Jumlah Barang" value="<?= $value["jumlah_barang"] ?>" required />
                </div>
                <div class="form-group">
                  <input type="submit" name="submit" class="form-control btn btn-success"
                    value="Update Record" />
                </div>
                <?php } ?>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>

</html>
```

## Soal No Ke 5

5. Buatlah script PHP untuk menampilkan form inputan dan melakukan insert data ke dalam tabel database (table_barang_masuk), dengan ketentuan seperti dibawah ini: [Bobot: 20] 
Script HTML untuk membuat input & output seperti gambar di bawah
Data pada dropdown kode_barang diisi dengan cara menarik data dari tabel_barang 
Script HTML PHP untuk membaca inputan, menyimpan ke database, serta mengeluarkan output ke layar seperti dibawah ini (Hanya Fitur Insert)

```php=
<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_npm1234";

$connection = new mysqli($servername, $username, $password, $database);

if ($connection->connect_error) {
  echo die("Connection Failed: " . $connection->connect_error);
}

// initialize all function method
insertData($connection);

function insertData($conn) {
  if(isset($_POST["submit"])) {
  $kode_barang_masuk = $_POST["kode_barang_masuk"];
  $kode_barang = $_POST["kode_barang"];
  $nama_barang = $_POST["nama_barang"];
  $jumlah_barang_masuk = $_POST["jumlah_barang_masuk"];

  $query = "INSERT INTO table_barang_masuk(kode_barang_masuk, kode_barang, nama_barang, jumlah_barang_masuk)
  VALUES('$kode_barang_masuk','$kode_barang','$nama_barang','$jumlah_barang_masuk')";

    if($conn->query($query) === TRUE) {
      alert("Insert new record successfully");
    } else {
      alert("Insert new record failed");
    }

  }
}

function resultDataBarang($conn) {
  $query = "SELECT * FROM table_barang";
  $data = $conn->query($query);

  if($data->num_rows > 0) {
    return $data;
  }
}

function alert($msg) {
  echo "<script>alert('$msg');</script>";
}

?>
```

```htmlembedded=
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
  </head>

  <body>
    <div class="container mt-3">
      <div class="row d-flex flex-fill justify-content-center">
        <div class="col-6">
          <div class="card">
            <div class="card-header bg-white text-center">
              <h4>Input</h4>
            </div>
            <div class="card-body">
              <form autocomplete="off" method="POST">
                <div class="form-group">
                  <label for="kode_barang_masuk" class="font-weight-bold">Kode Barang Masuk</label>
                  <input type="text" name="kode_barang_masuk" class="form-control"
                    placeholder="Masukan Kode Barang" required />
                </div>
                <div class="form-group">
                  <label for="kode_barang" class="font-weight-bold">Kode Barang</label>
                  <select name="kode_barang" class="form-control" onchange="selectedData()" id="selected">
                    <?php foreach(resultDataBarang($connection) as $value) { ?>
                    <option value="<?= $value["kode_barang"] ?>" selected="<?= $value["nama_barang"] ?>">
                      <?= $value["kode_barang"] ?></option>
                    <?php } ?>
                  </select>
                </div>
                <div class="form-group">
                  <label for="nama_barang" class="font-weight-bold">Nama Barang</label>
                  <input type="text" name="nama_barang" class="form-control" id="nama_barang"
                    placeholder="Masukan Harga Barang" required disabled />
                </div>
                <div class="form-group">
                  <label for="jumlah_barang_masuk" class="font-weight-bold">Jumlah Barang Masuk</label>
                  <input type="number" name="jumlah_barang_masuk" class="form-control"
                    placeholder="Masukan Jumlah Barang" required />
                </div>
                <div class="form-group">
                  <input type="submit" name="submit" class="form-control btn btn-primary"
                    value="Add Record" />
                </div>
              </form>
            </div>
          </div>
        </div>
        <dvi class="col-6">
          <div class="card">
            <div class="card-header bg-white text-center">
              <h4>Output</h4>
            </div>
            <div class="card-body">
              <h4 class="text-center">Daftar Barang Masuk</h4>
              <ul class="list-group mt-3">
                <li class="list-group-item">
                  <td><b>kode barang masuk:</b>
                  <td><?= $_POST["kode_barang_masuk"];?></td>
                  </td>
                </li>
                <li class="list-group-item">
                  <td><b>kode barang:</b>
                  <td><?= $_POST["kode_barang"];?></td>
                  </td>
                </li>
                <li class="list-group-item">
                  <td><b>nama barang:</b>
                  <td><?= $_POST["nama_barang"];?></td>
                  </td>
                </li>
                <li class="list-group-item">
                  <td><b>jumlah barang masuk:</b>
                  <td><?= $_POST["jumlah_barang_masuk"];?></td>
                  </td>
                </li>
              </ul>
            </div>
        </dvi>
      </div>
    </div>

  </body>

</html>

<script>
function selectedData() {
  const selectedValue = document.getElementById('selected')
  const data = selectedValue.querySelector("select option").getAttribute("selected")
  document.getElementById('nama_barang').value = data
}
</script>
```