- Add.html
<head>
<title>Form Create Data</title>
</head>
<body>
<form method="POST" action="create.php">
<table>
<tr>
<td>Title</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Content</td>
<td><textarea name="content"></textarea></td>
</tr>
<tr>
<td>Penulis</td>
<td><input type="text" name="penulis"></td>
</tr>
<tr>
<td></td>
<td><button type="submit" value="Add">Create</button></td>
</tr>
</table>
</form>
</body>
</html>
- config.php
<?php
//nama $host bisa diganti sesuai dengan kehendak kita, jika dipakai sendiri
$host = "localhost";
$user = "root";
$pass = "";
$db = "pelatihan2";
$db = "pelatihan2";
$connect = mysqli_connect($host, $user, $pass) or die ("connection failed!");
mysqli_select_db($connect,$db) or die ("database not found!");
?>
- create.php
<?php
include 'config.php';
$title = $_POST['title'];
$content = $_POST['content'];
$penulis = $_POST['penulis'];
$result = mysqli_query($connect, "INSERT INTO articles(title,content,penulis) VALUES('$title', '$content', '$penulis')");
if ($result){
header('location:index.php');
} else {
mysqli_error();
}
?>
- delete.php
<?php
include 'config.php';
$id = $_GET['id'];
$result = mysqli_query($connect, "DELETE FROM articles WHERE id=$id");
if($result){
header('location:index.php');
} else{
echo "Error";
}
?>
- edit.php
<?php
include 'config.php';
$id = $_GET['id'];
$result = mysqli_query($connect, "SELECT * FROM articles WHERE id=$id");
$data = mysqli_fetch_assoc($result);
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<form method="POST" action="update.php">
<table>
<tr>
<td>Title</td>
<td><input type="text" value="<?php echo $data['title']; ?>" name="title"></td>
</tr>
<tr>
<td>Content</td>
<td><textarea name="content"><?php echo $data['content']; ?></textarea></td>
</tr>
<tr>
<td>Penulis</td>
<td><input type="text" value="<?php echo $data['penulis']; ?>" name="penulis"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id ?>"></td>
<td><button type="submit" value="update">Update</button></td>
</tr>
</table>
</form>
</body>
</html>
- index.php
<?php
include 'config.php';
$result = mysqli_query($connect, "SELECT * FROM articles");
?>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td>No</td>
<td>Title</td>
<td>Content</td>
<td>Penulis</td>
<td>Action</td>
</tr>
<?php
$no = 1;
while ($data = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$data['title']."</td>";
echo "<td>".$data['content']."</td>";
echo "<td>".$data['penulis']."</td>";
echo "<td>
<a href=\"edit.php?id=$data[id]\">Edit</a> |
<a href=\"delete.php?id=$data[id]\"onClick=\"return confirm('Are u Sure?')\">Delete</a></td>";
echo "</tr>";
$no++;
}
?>
</table>
</body>
</html>
- update.php
<?php
include 'config.php';
$id= $_POST['id'];
$title= $_POST['title'];
$content= $_POST['content'];
$penulis= $_POST['penulis'];
$update = mysqli_query($connect, "UPDATE articles SET title='$title', content='$content', penulis='$penulis' WHERE id='$id'");
if($update){
header('location:index.php');
}
else{
echo "Error";
}
?>