SCRIPT UPRAK 2024

catur wahyu nugroho

catur wahyu nugroho

Nov 17, 2024

file index.php
<?php
// Konfigurasi koneksi database
$host = "alamat end point RDS"; // Host database
$dbname = "nama db yang dibuat"; // Ganti dengan nama database Anda
$user = "username"; // Ganti dengan username PostgreSQL Anda
$password = "password rds"; // Ganti dengan password PostgreSQL Anda
try {
// Membuat koneksi ke PostgreSQL
$pdo = new PDO("pgsql:host=$host;dbname=$dbname", $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Cek jika tombol beli ditekan
if (isset($_POST['buy_id'])) {
$buy_id = $_POST['buy_id'];
// Query untuk mendapatkan stok produk yang dibeli
$query = "SELECT stok FROM barang WHERE id = :id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':id', $buy_id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row && $row['stok'] > 0) {
// Mengurangi stok barang
$new_stok = $row['stok'] - 1;
$updateQuery = "UPDATE barang SET stok = :stok WHERE id = :id";
$updateStmt = $pdo->prepare($updateQuery);
$updateStmt->bindParam(':stok', $new_stok, PDO::PARAM_INT);
$updateStmt->bindParam(':id', $buy_id, PDO::PARAM_INT);
$updateStmt->execute();
echo "<script>alert('Barang berhasil dibeli!');</script>";
} else {
echo "<script>alert('Stok tidak cukup!');</script>";
}
}
// Query untuk mengambil data produk
$query = "SELECT * FROM barang ORDER BY id ASC";
$stmt = $pdo->prepare($query);
$stmt->execute();
} catch (PDOException $e) {
// Menampilkan pesan error jika koneksi gagal
echo "Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toko Online - Produk Kami</title>
<link rel="stylesheet" href="styles.css"> <!-- External CSS -->
</head>
<body>
<header>
<div class="container">
<h1>Toko Elektronik</h1>
<nav>
<ul>
<li><a href="#">Beranda</a></li>
<li><a href="#">Kategori</a></li>
<li><a href="#">Kontak</a></li>
</ul>
</nav>
</div>
</header>
<main>
<div class="container">
<h2>Produk Kami</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Nama Barang</th>
<th>Kategori</th>
<th>Harga</th>
<th>Stok</th>
<th>Tanggal Ditambahkan</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<td><?php echo $row['kategori']; ?></td>
<td>Rp <?php echo number_format($row['harga'], 2, ',', '.'); ?></td>
<td><?php echo $row['stok']; ?></td>
<td><?php echo $row['tanggal_ditambahkan']; ?></td>
<td>
<!-- Tombol beli -->
<form method="POST" style="display:inline;">
<input type="hidden" name="buy_id" value="<?php echo $row['id']; ?>">
<button type="submit">Beli</button>
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</main>
<footer>
<div class="container">
<p>&copy; 2024 Toko Elektronik. Semua Hak Dilindungi.</p>
</div>
</footer>
</body>
</html>

file styles.css

/* Reset beberapa default styling */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
color: #333;
}
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Header Styling */
header {
background-color: #333;
color: white;
padding: 20px 0;
margin-bottom: 40px;
}
header h1 {
text-align: center;
font-size: 2.5rem;
}
header nav ul {
list-style: none;
display: flex;
justify-content: center;
margin-top: 10px;
}
header nav ul li {
margin: 0 15px;
}
header nav ul li a {
color: white;
text-decoration: none;
font-size: 1.1rem;
}
/* Main Content Styling */
main {
padding: 20px 0;
background-color: #fff;
border-radius: 8px;
}
h2 {
text-align: center;
margin-bottom: 30px;
font-size: 2rem;
color: #555;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table th, table td {
padding: 15px;
text-align: center;
border: 1px solid #ddd;
}
table th {
background-color: #007bff;
color: white;
}
table tr:nth-child(even) {
background-color: #f9f9f9;
}
table tr:hover {
background-color: #f1f1f1;
}
table td {
color: #555;
}
/* Footer Styling */
footer {
background-color: #333;
color: white;
padding: 20px 0;
text-align: center;
margin-top: 40px;
}