Skip to content

Commit f8dd1ce

Browse files
committed
Completed the lesson on using PHP to insert, update and delete data stored in MySQL databases
1 parent d518e68 commit f8dd1ce

File tree

8 files changed

+142
-10
lines changed

8 files changed

+142
-10
lines changed

website8/addpost.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
require_once 'config/config.php';
3+
require_once 'config/db.php';
4+
5+
// Check For Submit
6+
if (isset($_POST['submit'])) {
7+
// Get Form Data
8+
$title = mysqli_real_escape_string($conn, $_POST['title']);
9+
$body = mysqli_real_escape_string($conn, $_POST['body']);
10+
$author = mysqli_real_escape_string($conn, $_POST['author']);
11+
12+
$query = "INSERT INTO post(title, author, body) VALUES('$title', '$author', '$body')";
13+
14+
if(mysqli_query($conn, $query)) {
15+
header('Location: '.ROOT_URL.'');
16+
} else {
17+
echo 'ERROR: '.mysqli_error($conn);
18+
}
19+
}
20+
?>
21+
<?php include_once 'inc/header.php'; ?>
22+
<div class="container">
23+
<h1>Add Post</h1>
24+
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
25+
<div class="form-group">
26+
<label>Title</label>
27+
<input type="text" name="title" class="form-control">
28+
</div>
29+
<div class="form-group">
30+
<label>Author</label>
31+
<input type="text" name="author" class="form-control">
32+
</div>
33+
<div class="form-group">
34+
<label>Body</label>
35+
<textarea name="body" class="form-control"></textarea>
36+
</div>
37+
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
38+
</form>
39+
</div>
40+
<?php include_once 'inc/footer.php'; ?>

website8/config/config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
define('DB_HOST', 'localhost');
44
define('DB_USER', 'root');
55
define('DB_PASS', 'SuyashD95');
6-
define('DB_NAME', 'phpblog');
7-
?>
6+
define('DB_NAME', 'phpblog');

website8/config/db.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
if (mysqli_connect_errno()) {
77
// Connection Failed
88
echo 'Failed to connect to MySQL'.mysqli_connect_errno();
9-
}
10-
?>
9+
}

website8/editpost.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
require_once 'config/config.php';
3+
require_once 'config/db.php';
4+
5+
// Check For Submit
6+
if (isset($_POST['update_id'])) {
7+
// Get Form Data
8+
$update_id = mysqli_real_escape_string($conn, $_POST['update_id']);
9+
$title = mysqli_real_escape_string($conn, $_POST['title']);
10+
$body = mysqli_real_escape_string($conn, $_POST['body']);
11+
$author = mysqli_real_escape_string($conn, $_POST['author']);
12+
13+
$query = "UPDATE post SET
14+
title='$title',
15+
author='$author',
16+
body='$body'
17+
WHERE id={$update_id}";
18+
19+
if(mysqli_query($conn, $query)) {
20+
header('Location: '.ROOT_URL.'');
21+
} else {
22+
echo 'ERROR: '.mysqli_error($conn);
23+
}
24+
}
25+
26+
// Get Id
27+
$id = mysqli_real_escape_string($conn, $_GET['id']);
28+
29+
// Create Query
30+
$query = 'SELECT * FROM post WHERE id='.$id;
31+
32+
// Get Result
33+
$result = mysqli_query($conn, $query);
34+
35+
// Fetch Data
36+
$post = mysqli_fetch_assoc($result);
37+
// var_dump($posts);
38+
39+
// Free Result
40+
mysqli_free_result($result);
41+
42+
// Close Connection
43+
mysqli_close($conn);
44+
?>
45+
<?php include_once 'inc/header.php'; ?>
46+
<div class="container">
47+
<h1>Add Post</h1>
48+
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
49+
<div class="form-group">
50+
<label>Title</label>
51+
<input type="text" name="title" class="form-control" value="<?php echo $post['title']; ?>">
52+
</div>
53+
<div class="form-group">
54+
<label>Author</label>
55+
<input type="text" name="author" class="form-control" value="<?php echo $post['author']; ?>">
56+
</div>
57+
<div class="form-group">
58+
<label>Body</label>
59+
<textarea name="body" class="form-control"><?php echo $post['body']; ?></textarea>
60+
</div>
61+
<input type="hidden" name="update_id" value="<?php echo $post['id']; ?>">
62+
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
63+
</form>
64+
</div>
65+
<?php include_once 'inc/footer.php'; ?>

website8/inc/footer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
<script
2+
src="https://code.jquery.com/jquery-3.3.1.min.js"
3+
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
4+
crossorigin="anonymous"></script>
5+
6+
<script
7+
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"
8+
integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T"
9+
crossorigin="anonymous"></script>
110
</body>
211
</html>

website8/inc/navbar.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
88
<ul class="navbar-nav mr-auto">
9-
<li>
10-
<a class="nav-link" href="<?php echo ROOT_URL; ?>">Home <span class="sr-only">(current)</span></a>
11-
</li>
9+
<li><a class="nav-link" href="<?php echo ROOT_URL; ?>">Home</a></li>
10+
<li><a class="nav-link" href="<?php echo ROOT_URL; ?>addpost.php">Add Post</a></li>
1211
</div>
1312
</nav>

website8/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_once 'config/db.php';
44

55
// Create Query
6-
$query = 'SELECT * FROM post';
6+
$query = 'SELECT * FROM post ORDER BY created_at DESC';
77

88
// Get Result
99
$result = mysqli_query($conn, $query);

website8/post.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
<?php
2-
require_once 'config/config.php';
3-
require_once 'config/db.php';
2+
require 'config/config.php';
3+
require 'config/db.php';
4+
5+
// Check For Submit
6+
if (isset($_POST['delete'])) {
7+
// Get Form Data
8+
$delete_id = mysqli_real_escape_string($conn, $_POST["delete_id"]);
9+
10+
$query = "DELETE FROM post WHERE id = $delete_id";
11+
12+
if (mysqli_query($conn, $query)) {
13+
header('Location: '.ROOT_URL.'');
14+
} else {
15+
echo 'ERROR: '.mysqli_error($conn);
16+
}
17+
}
418

519
// Get Id
620
$id = mysqli_real_escape_string($conn, $_GET['id']);
@@ -27,5 +41,12 @@
2741
<h1><?php echo $post['title']; ?></h1>
2842
<small>Created on <?php echo $post['created_at']; ?> by <?php echo $post['author']; ?></small>
2943
<p><?php echo $post['body']; ?></p>
44+
<hr>
45+
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
46+
<input type="hidden" name="delete_id" value="<?php echo post['id']; ?>">
47+
<input type="submit" name="delete" value="Delete" class="btn btn-danger">
48+
</form>
49+
50+
<a href="<?php echo ROOT_URL; ?>editpost.php?id=<?php echo $post['id']; ?>" class="btn btn-default">Edit</a>
3051
</div>
3152
<?php include_once 'inc/footer.php'; ?>

0 commit comments

Comments
 (0)