Modul Basis Data
Modul Basis Data
html
Install Xampp
Klik Next
Klik Next
Klik Next
Klik Next
Klik Finish
Buka XAMPP
Buat File php dengan cara klik kanan New File dengan nama create_db_table.php
<?php
// Database connection credentials
$host = 'localhost';
$user = 'root'; // Default XAMPP username
$password = ''; // Default XAMPP password
// Connect to MySQL
$conn = new mysqli($host, $user, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create Database
$dbName = 'Mahasiswa';
$sqlCreateDB = "CREATE DATABASE IF NOT EXISTS $dbName";
if ($conn->query($sqlCreateDB) === TRUE) {
echo "Database created successfully!<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}
// Create Table
$tableName = 'users';
$sqlCreateTable = "
CREATE TABLE IF NOT EXISTS $tableName (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL
)";
if ($conn->query($sqlCreateTable) === TRUE) {
echo "Table '$tableName' created successfully!";
} else {
echo "Error creating table: " . $conn->error;
}
// Close connection
$conn->close();
?>
Buka google chrome, ketik di pencarian localhost/Database/create_db_table.php
<?php
$host = 'localhost';
$dbName = 'Mahasiswa';
$user = 'root';
$password = '';
// Create connection
$conn = new mysqli($host, $user, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Buat file baru dengan nama create.php
<?php
include 'connection.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
<!DOCTYPE html>
<html>
<head>
<title>Create User</title>
</head>
<body>
<h2>Create User</h2>
<form method="POST">
<label>Name:</label><br>
<input type="text" name="name" required><br><br>
<label>Email:</label><br>
<input type="email" name="email" required><br><br>
<button type="submit">Create</button>
</form>
</body>
</html>
<?php
include 'connection.php';
<!DOCTYPE html>
<html>
<head>
<title>All Users</title>
</head>
<body>
<h2>All Users</h2>
<a href="create.php">Add New User</a><br><br>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['email'] ?></td>
<td>
<a href="update.php?id=<?= $row['id'] ?>">Edit</a>
<a href="delete.php?id=<?= $row['id'] ?>" onclick="return
confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
Ketik di pencarian localhost/Database/create.php untuk menjalankan perintah create.php
Jika berhasil akan muncul tampilan seperti ini
<?php
include 'connection.php';
$id = $_GET['id'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
<!DOCTYPE html>
<html>
<head>
<title>Update User</title>
</head>
<body>
<h2>Update User</h2>
<form method="POST">
<label>Name:</label><br>
<input type="text" name="name" value="<?= $user['name'] ?>"
required><br><br>
<label>Email:</label><br>
<input type="email" name="email" value="<?= $user['email'] ?>"
required><br><br>
<button type="submit">Update</button>
</form>
</body>
</html>
Buat file baru dengan nama delete.php
<?php
include 'connection.php';
$id = $_GET['id'];