Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blog Functionality this PHP Blog Website #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
$hostname="localhost";
$username="root";
$password="";
$database="blog_data_phpv1";

$conn = mysqli_connect($hostname,$username,$password,$database);

if(!$conn){
echo "<h3 class='container bg-secondary text-center p-3 text-warning rounded mt-5'>Not able to establish connection</h3>";
}
?>
35 changes: 6 additions & 29 deletions create.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
<?php
include "logic.php"
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<!-- bootstrap css -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- my css -->
<link rel="stylesheet" href="css/style.css">

</head>
<body class="bg-dark">
<?php include("inc/header.php");?>
<div class="container mt-5">
<form method="POST">
<form method="POST" action="logic.php">
<label for="blog title" class="text-light border-left-0 customText">Blog Title</label>
<input type="text" placeholder = "Blog Title" class="form-control my-3 bg-secondary text-light border-0">
<input type="text" placeholder = "Blog Title" name="title" class="form-control my-3 bg-secondary text-light border-0" required>
<label for="content" class="text-light customText">Content</label>
<textarea cols="20" rows="5" class="form-control my-3 bg-secondary text-light border-0"></textarea>
<button class="btn btn-outline-warning">Add Post</button>
<textarea cols="20" rows="5" name="content" class="form-control my-3 bg-secondary text-light border-0" required></textarea>
<input type="submit" value="Add Post" class="btn btn-outline-warning">
</form>
</div>
<!-- bootstrap js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>
<?php include("inc/footer.php") ?>
71 changes: 71 additions & 0 deletions db/blog_data_phpv1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Oct 13, 2021 at 05:11 PM
-- Server version: 10.4.18-MariaDB
-- PHP Version: 7.4.16

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `blog_data_phpv1`
--

-- --------------------------------------------------------

--
-- Table structure for table `blogs`
--

CREATE TABLE `blogs` (
`id` int(11) NOT NULL,
`blog_title` text NOT NULL,
`blog_content` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `blogs`
--

INSERT INTO `blogs` (`id`, `blog_title`, `blog_content`) VALUES
(1, 'Ut explicabo Fugiat', 'Ut laboriosam in in'),
(2, 'Ut quos tempora in a', 'Mollitia voluptas ei'),
(3, 'Id esse explicabo ', 'Sunt doloremque non'),
(4, 'fdsfdsf', 'sdfsdfsdfsfsdff'),
(5, 'Aut irure ipsum ven', 'Voluptatem beatae am'),
(6, 'Laboriosam doloribu', 'Et sed cum odio inci');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `blogs`
--
ALTER TABLE `blogs`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `blogs`
--
ALTER TABLE `blogs`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
10 changes: 10 additions & 0 deletions inc/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
unset($_SESSION['message']);
unset($_SESSION['alert']);
?>
<!-- bootstrap js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions inc/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<!-- bootstrap css -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- my css -->
<link rel="stylesheet" href="css/style.css">

</head>
<body class="bg-dark">
34 changes: 11 additions & 23 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<?php
include "logic.php"
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog using php and mysql</title>
<!-- bootstrap css -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- my css -->
<link rel="stylesheet" href="css/style.css">
</head>
<body class="bg-dark">
<?php include("inc/header.php");?>
<div class="contianer mt-5">
<?php
if(isset($_SESSION['message']) && $_SESSION['message']!=''){
?>
<div class="alert alert-<?php echo $_SESSION['alert']; ?> col-md-4 m-auto my-2" role="alert">
<?php echo $_SESSION['message']; ?>
</div>
<?php
}
?>
<div class="text-center">
<a href="create.php" class= "btn btn-outline-light"> + create a new post </a>
</div>
</div>

<!-- bootstrap js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>
<?php include("inc/footer.php") ?>
27 changes: 21 additions & 6 deletions logic.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
<?php
$conn = mysqli_connect("localhost","root", "secret", "blog_data_phpv1");

if(!$conn){
echo "<h3 class='container bg-secondary text-center p-3 text-warning rounded mt-5'>Not able to establish connection</h3>";
<?php session_start();
require("config.php");
$_SESSION['message']=array();
$_SESSION['alert']='success';
if(!empty($_POST)){

$title=mysqli_escape_string($conn,$_POST['title']);
$content=mysqli_escape_string($conn,$_POST['content']);

$query=" INSERT INTO blogs (`blog_title`,`blog_content`) VALUES ('$title','$content')";

if(!mysqli_query($conn,$query)){
$_SESSION['message']='Something Went Wrong';
$_SESSION['alert']='danger';
}else{
$_SESSION['message']='Blog Inserted';

}
header("location:index.php");
}
?>

?>