sql/phpmyadminCREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL, phone VARCHAR(15) NOT NULL);index.php<!DOCTYPE html><html lang=”en”><head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>CRUD PHP Procedural</title> <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css”></head><body> <div class=”container”> <h2>CRUD PHP Procedural</h2> <a href=”create.php” class=”btn btn-primary mb-3″>Tambah User</a> <table class=”table”> <thead> <tr> <th>ID</th> <th>Nama</th> <th>Email</th> <th>Telepon</th> <th>Aksi</th> </tr> </thead> <tbody> <?php include ‘config.php’; $sql = “SELECT * FROM users”; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo “<tr>”; echo “<td>”.$row[‘id’].”</td>”; echo “<td>”.$row[‘name’].”</td>”; echo “<td>”.$row[‘email’].”</td>”; echo “<td>”.$row[‘phone’].”</td>”; echo “<td> <a href=’edit.php?id=”.$row[‘id’].”‘ class=’btn btn-primary’>Edit</a> <a href=’delete.php?id=”.$row[‘id’].”‘ class=’btn btn-danger’>Delete</a> </td>”; echo “</tr>”; } } else { echo “<tr><td colspan=’5′>Tidak ada data</td></tr>”; } ?> </tbody> </table> </div></body></html>config.php<?php$conn = mysqli_connect(“localhost”, “username”, “password”, “crud_db”);if (!$conn) { die(“Connection failed: ” . mysqli_connect_error());}?>create.php<?phpinclude ‘config.php’;if ($_SERVER[“REQUEST_METHOD”] == “POST”) { $name = $_POST[‘name’]; $email = $_POST[‘email’]; $phone = $_POST[‘phone’]; $sql = “INSERT INTO users (name, email, phone) VALUES (‘$name’, ‘$email’, ‘$phone’)”; if (mysqli_query($conn, $sql)) { header(“location: index.php”); } else { echo “Error: ” . $sql . “<br>” . mysqli_error($conn); }}?><!DOCTYPE html><html lang=”en”><head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Tambah User</title> <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css”></head><body> <div class=”container”> <h2>Tambah User</h2> <form method=”post” action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>”> <div class=”form-group”> <label>Nama:</label> <input type=”text” class=”form-control” name=”name” required> </div> <div class=”form-group”> <label>Email:</label> <input type=”email” class=”form-control” name=”email” required> </div> <div class=”form-group”> <label>Telepon:</label> <input type=”text” class=”form-control” name=”phone” required> </div> <button type=”submit” class=”btn btn-primary”>Tambah</button> </form> </div></body></html>edit.php<?phpinclude ‘config.php’;if ($_SERVER[“REQUEST_METHOD”] == “POST”) { $id = $_POST[‘id’]; $name = $_POST[‘name’]; $email = $_POST[‘email’]; $phone = $_POST[‘phone’]; $sql = “UPDATE users SET name=’$name’, email=’$email’, phone=’$phone’ WHERE id=’$id'”; if (mysqli_query($conn, $sql)) { header(“location: index.php”); } else { echo “Error updating record: ” . mysqli_error($conn); }}$id = $_GET[‘id’];$sql = “SELECT * FROM users WHERE id=’$id'”;$result = mysqli_query($conn, $sql);$row = mysqli_fetch_assoc($result);?><!DOCTYPE html><html lang=”en”><head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Edit User</title> <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css”></head><body> <div class=”container”> <h2>Edit User</h2> <form method=”post” action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>”> <input type=”hidden” name=”id” value=”<?php echo $row[‘id’]; ?>”> <div class=”form-group”> <label>Nama:</label> <input type=”text” class=”form-control” name=”name” value=”<?php echo $row[‘name’]; ?>” required> </div> <div class=”form-group”> <label>Email:</label> <input type=”email” class=”form-control” name=”email” value=”<?php echo $row[‘email’]; ?>” required> </div> <div class=”form-group”> <label>Telepon:</label> <input type=”text” class=”form-control” name=”phone” value=”<?php echo $row[‘phone’]; ?>” required> </div> <button type=”submit” class=”btn btn-primary”>Update</button> </form> </div></body></html>delete.php<?phpinclude ‘config.php’;$id = $_GET[‘id’];$sql = “DELETE FROM users WHERE id=$id”;if (mysqli_query($conn, $sql)) { header(“Location: index.php”);} else { echo “Error deleting record: ” . mysqli_error($conn);}mysqli_close($conn);?>

Terimakasih telah membaca di Aopok.com, semoga bermanfaat dan lihat juga di situs berkualitas dan paling populer Piool.com, peluang bisnis online Topbisnisonline.com dan join di komunitas Topoin.com.