CRUD adalah dimana kit bisa melakukan Create Read Update data. Dalam proses pembuatan website pasti kita membutuhkan CRUD yang digunakan dalam proses daftar(create), membaca(read), bahkan menghapus(delete). Pada pembuatan CRUD kita membutuhkan beberapa script agar CRUD bisa berjalan.
Pertama kita buat database dengan nama crud :
- -- Table structure for user
- -- ----------------------------
- CREATE TABLE `user` (
- `id` int(5) NOT NULL AUTO_INCREMENT,
- `username` varchar(30) NOT NULL,
- `password` varchar(30) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
- -- ----------------------------
- -- Records
- -- ----------------------------
- INSERT INTO `user` VALUES ('1', 'admin', '12345');
- INSERT INTO `user` VALUES ('5', 'onehied', '12345');
Kemudian buat fie baru dengan nama config.php :
- <?php
- $host = "localhost";
- $user = "root";
- $pass = "root";
- $db_name = "crud";
- mysql_connect($host, $user, $pass) or die (mysql_error());
- mysql_select_db($db_name) or die (mysql_error());
- ?>
Selanjutnya kita buat file baru dengan nama index.php :
- <html>
- <head><title>Cara Membuat CRUD</title></head>
- <link rel="stylesheet" type="text/css" href="style.css">
- <body>
- <div id="head"></div>
- <div id="header">
- <h2>Cara Membuat CRUD</h2>
- Oleh Onehied
- <div id="menu">
- <a href="index.php" class="active">Home</a>
- <a href="login.php">Login</a>
- </div>
- <div id="content">
- <p>Cara Membuat CRUD oleh <a href="http://www.darionehied.blogspot.com/" target="_blank"><strong>darionehied</strong></a>
- Anda bisa mempublikasikan ulang, atau merubah Source Code ini.
- </p>
- </div>
- </div>
- </body>
- </html>
- <html>
- <head><title>Cara Membuat CRUD</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <div id="head">
- <div id="header">
- <h2>Cara Membuat CRUD</h2>
- <span>Oleh Onehied</span>
- <div id="menu">
- <a href="index.php">Home</a>
- <a href="login.php" class="active">Login</a>
- </div><b>Sign in</b>
- <div id="content">
- <form action="ceklogin.php" method="post">
- <div id="acc1"><input type='text' name='username' size="30" placeholder='Username'></div>
- <div id="acc2"><input type='password' name='password' size="30" placeholder='Password'></div>
- <div id="acc3">
- <input name="submit" type="submit" value="Sign in">
- </div>
- <div id="acc4"><a href="create.php">Create</a></div><br><br>
- <div id="link"><a href="http://www.darionehied.blogspot.com/" target="_blank">darionehied</a></div>
- </form>
- </div>
- </div>
- </body>
- </html>
- <?php
- session_start();
- include 'config.php';
- if (!empty($_SESSION['username'])) {
- header('location:login.php');
- }
- if(!empty($_POST)){
- $username = $_POST['username'];
- $password =($_POST['password']);
- $sql = "select * from user where username='".$username."' and password='".$password."'";
- #echo $sql."<br />";
- $query = mysql_query($sql) or die (mysql_error());
- // pengecekan query valid atau tidak
- if($query){
- $row = mysql_num_rows($query);
- // jika $row > 0 atau username dan password ditemukan
- if($row > 0){
- $_SESSION['isLoggedIn']=1;
- $_SESSION['username']=$username;
- header('Location: dashboard.php');
- }else{
- ?><script>alert('Username atau password salah');</script><?
- }
- }
- }
- include'login.php';
- ?>
- <?php
- session_start();
- session_destroy();
- header('Location: index.php');
- ?>
- <html>
- <head><title>Cara Membuat CRUD</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <div id="head">
- <div id="header">
- <h2>Cara Membuat CRUD</h2>
- <span>Oleh Onehied</span>
- <div id="menu">
- <a href="index.php">Home</a>
- <a href="login.php" class="active">Login</a>
- </div><b>Create Account</b>
- <div id="content">
- <form action="cekcreate.php" method="post">
- <div id="acc1"><input type='text' name='username' size="30" placeholder='Username'></div>
- <div id="acc2"><input type='password' name='password' size="30" placeholder='Password'></div>
- <div id="acc3"><input type="submit" value="Create"></div><br><br>
- <div id="link"><a href="http://www.darionehied.blogspot.com/" target="_blank">darionehied</a></div>
- </form>
- </div>
- </div>
- </body>
- </html>
- <?php
- //panggil file config.php untuk menghubung ke server
- include('config.php');
- //tangkap data dari form
- $username = $_POST['username'];
- $password = $_POST['password'];
- $sql=mysql_query(" select * from user");
- $id=0;
- while ($row = mysql_fetch_array($sql))
- { if ($row['id'] <= $id)
- {
- $id=$id+1;
- }}
- //simpan data ke database
- $query = mysql_query("insert into user values('$id', '$username', '$password')") or die(mysql_error());
- if ($query) {
- // jika berhasil menyimpan
- header('location: index.php?msg=success');
- } else {
- // jika gagal menyimpan
- header('location: login.php?msg=failed');
- }
- ?>
- <?php
- include 'config.php';
- session_start();
- $username = $_SESSION['username'];
- $isLoggedIn = $_SESSION['isLoggedIn'];
- if($isLoggedIn != '1'){
- session_destroy();
- header('Location: login.php');
- }
- ?>
- <html>
- <head>
- <title>Cara Membuat CRUD</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head><body>
- <div id="container">
- <div id="header">
- <h2>Cara Membuat CRUD</h2>
- <span>Oleh Onehied</span>
- <div id="menu">
- <a href="index.php">Home</a>
- <a href="login.php">Login</a>
- <a href="dashboard.php" class="active">Dasboard</a>
- <a href="logout.php">Logout</a>
- </div>
- <div id="content">
- <p><b>Selamat <?php echo $username; ;?> berhasil login.</b></p>
- <table width="100%">
- <tr>
- <td align="center" width="50"><b>Opsi</b></td>
- <td align="center"><b>No</b></td>
- <td align="center"><b>Username</b></td>
- <td align="center"><b>Password</b></td>
- </tr>
- <?php
- $sql = "select * FROM user";
- $no = 1;
- $tampil = mysql_query($sql);
- while ($data = mysql_fetch_array($tampil)){
- ?>
- <tr>
- <td align="center"><?php echo $id ?>
- <a href="delete.php?id=<?php echo $data['id']; ?>"><img src="images/delete.gif"></a>
- <a href="edit.php?id=<?php echo $data['id']; ?>"><img src="images/edit.png"></a></td>
- <td align="center"><?php echo $no; ?></td>
- <td align="center"><?php echo $data['username']; ?></td>
- <td align="center"><?php echo $data['password']; ?></td>
- </tr>
- <?php
- $no++;
- }
- ?>
- </table>
- <div id="link"><a href="http://www.darionehied.blogspot.com/" target="_blank">darionehied</a></div>
- </div>
- </div>
- </div>
- </body>
- </html>
- <?php
- include('config.php');
- $id = $_GET['id'];
- $query = mysql_query("delete from user where id='$id'") or die(mysql_error());
- if ($query) {
- header('location:dashboard.php?message=delete');
- }
- ?>
- <?php
- include 'config.php';
- session_start();
- $username = $_SESSION['username'];
- $isLoggedIn = $_SESSION['isLoggedIn'];
- if($isLoggedIn != '1'){
- session_destroy();
- header('Location: login.php');
- }
- ?>
- <html>
- <head>
- <title>Cara Membuat CRUD</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <div id="container">
- <div id="header">
- <h2>Cara Membuat CRUD</h2>
- <span>Oleh Onehied</span>
- <div id="menu">
- <a href="index.php">Home</a>
- <a href="login.php">Login</a>
- <a href="dashboard.php" class="active">Dasboard</a>
- <a href="logout.php">Logout</a>
- </div>
- <div id="content">
- <?php
- $id = $_GET['id'];
- $query = mysql_query("select * from user where id='$id'") or die(mysql_error());
- $data = mysql_fetch_array($query);
- ?>
- <form name="update" action="update.php" method="post">
- <table width="100%">
- <tr>
- <td align="center"><b>Username</b></td>
- <td align="center"><b>Password</b></td>
- </tr>
- <tr>
- <input type="hidden" name="id" value="<?php echo $id; ?>" />
- <td align="center"><input type="text" name="username" maxlength="20" required="required" value="<?php echo $data['username']; ?>" /></td>
- <td align="center"><input type="text" name="password" maxlength="20" required="required" value="<?php echo $data['password']; ?>" /></td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <input type="submit" name="submit" value="Update" />
- </td>
- </tr>
- </form>
- </table>
- <div id="link"><a href="http://www.darionehied.blogspot.com/" target="_blank">darionehied</a></div>
- </div>
- </div>
- </div>
- </body>
- </html>
- <?php
- include('config.php');
- //tangkap data dari form
- $id = $_POST['id'];
- $username = $_POST['username'];
- $password = $_POST['password'];
- //update data di database sesuai id
- $query = mysql_query("update user set username='$username', password='$password' where id='$id'") or die(mysql_error());
- if ($query) {
- header('location:dashboard.php?message=success');
- }
- ?>
- body {
- background-color:none;
- }
- #header {
- width:550px;
- margin:20px auto;
- padding:10px;
- text-align:center;
- background-color:#fff;
- box-shadow:0px 0px 3px #000;
- }
- h1, h2, h3 {
- margin:0;
- padding:0;
- }
- #menu {
- text-align:center;
- margin:15px 0px;
- border-top:1px solid #0099FF;
- border-bottom:1px solid #0099FF;
- }
- #menu a {
- display:inline-block;
- padding:5px 10px;
- text-decoration:none;
- color:#000;
- font-weight:bold;
- }
- #menu a:hover {
- background-color:#0099FF;
- }
- #menu a.active {
- background-color:#0099FF;
- }
- .table, td {
- border:1px solid #0099FF}
- #acc1 {
- margin:10px 0px 0px 0px;
- }
- input[type=text] {
- color:#ffff;
- height: 20px;
- }
- #acc2 {
- margin:10px 0px 0px 0px;
- }
- input[type=password] {
- color:#ffff;
- height: 20px;
- }
- #acc4 {
- text-align:left;
- margin:-22px 0px 0px 180px;
- }
- #link {
- text-align:right;
- }
- input[type=submit] {
- color:#ffff;
- width:80px;
- height: 25px;
- background:#3399FF;
- margin:0px 0px 0px 0px;
- border-radius:18px 18px 18px 18px;
- }
- a, a:link, a:visited { color:#0099FF; font-weight: normal; text-decoration: none;}
- a:hover }
Download Source Code
Download
Password
darionehied

Posting Komentar