Wednesday, March 18, 2020

PHP CRUD Application Project Code

Dear Learner Thanks for being here.Our main focus is to make much more easy to learn programming language to all of you. That's why we have make our Youtube Chanel with name of Tutor-Programming You can go through this link and check for Our Latest video upload watch !!! enjoy and Learn from there There Only one request to you that please subscribe and like to that video your like and subscribe make me proud so that we make such a osam video to all of you.
As we have make an video on PHP CRUD App in hindi on live Example.
Must Watch to that series: Watch PHP CRUD Video Series in Hindi

Steps 1:
Create Database new_php_crud

Steps 2

Copy the Following Code and Paste into you mysql query BOX:-


-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 18, 2020 at 05:35 PM
-- Server version: 10.1.36-MariaDB
-- PHP Version: 7.2.11

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
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: `new_php_crud`
--

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

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

CREATE TABLE `blogs` (
  `id` int(11) NOT NULL,
  `title` varchar(500) NOT NULL,
  `description` varchar(5000) NOT NULL,
  `image` varchar(100) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `userid` int(11) NOT NULL,
  `posted_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

INSERT INTO `blogs` (`id`, `title`, `description`, `image`, `status`, `userid`, `posted_at`) VALUES
(1, 'My Title', 'DescDescDescDescDescDescDescDesc', 'bg.jpg', 1, 0, '2020-03-15 17:47:21'),
(3, 'Hello My Blog No 1 After Modify', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim', 'bg.jpg', 1, 1, '2020-03-15 19:05:50');

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

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(25) NOT NULL,
  `email` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `name`, `email`, `password`, `status`, `created_at`) VALUES
(1, 'Mayank', 'mayank@domain.com', '123', 1, '2020-03-15 16:55:25');

--
-- Indexes for dumped tables
--

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

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique` (`email`);

--
-- AUTO_INCREMENT for dumped tables
--

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

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

header.php
<?php include 'db/config.php'; ?>
<?php 
function show_user($con,$id)
{
$sql="select * from users where id='".$id."'";
$res=mysqli_query($con,$sql);
$row_user=mysqli_fetch_assoc($res);
return $row_user;
}
 ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container" style="margin-top: 80px;">
<div class="row">
<div class="col-md-12">

<nav class="navbar  bg-info">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="index.php">NEW PHP CRUD</a>
    </div>
    <ul class="nav navbar-nav">
        <li><a href="index.php">Home</a></li>
<?php 
if (@$_SESSION["userid"]=="") { ?>
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Registeration</a></li>
<?php }
else { ?>
<li><a href="admin/index.php">Profile</a></li>
<li><a href="logout.php">Logout</a></li>
<?php }

?>
    </ul>
  </div>
</nav>
</div>
</div>
</div>
footer.php
<!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </body> </html>

index.php

<?php include 'header.php'; ?>
<div class="container">
<?php 
$sql_blog="select * from blogs where status='1'";
$result=mysqli_query($con,$sql_blog);

while ($blogs=mysqli_fetch_assoc($result)) { ?>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3><?php echo $blogs["title"]; ?></h3>
</div>
<div class="panel-body">
<p><?php echo $blogs["description"]; ?></p>
<p><?php echo '<a href="blog.php?id='.$blogs["id"].'" class="btn btn-default">Read More</a>' ?></p>
</div>
<div class="panel-footer">
<small><?php echo $blogs["posted_at"]; ?> , <q>Posted By- <?php $users=show_user($con,$blogs["userid"]);echo $users["name"]; ?></q></small>
</div>
</div>
</div>
</div>
<?php }
?>
</div>
<?php include 'footer.php'; ?>

==============================================================================
Login.php

<?php include 'header.php'; ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php 
if (isset($_SESSION["success"])) {
echo "<p class='alert alert-success'>".$_SESSION["success"]."</p>";
unset($_SESSION["success"]);
}
if (isset($_SESSION["error"])) {
echo "<p class='alert alert-danger'>".$_SESSION["error"]."</p>";
unset($_SESSION["error"]);
}
?>
<form method="post" action="db/controller.php">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Login Page</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label>Email</label>
<input type="email" name="email" placeholder="Email" class="form-control" />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" placeholder="Password" class="form-control" />
</div>
</div>
<div class="panel-footer">
<input type="submit" name="login" value="Login" class="btn btn-default">
New User <a href="register.php">Register</a>
</div>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
===============================================================================
Register.php
<?php include 'header.php'; ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php 
if (isset($_SESSION["success"])) {
echo "<p class='alert alert-success'>".$_SESSION["success"]."</p>";
unset($_SESSION["success"]);
}
if (isset($_SESSION["error"])) {
echo "<p class='alert alert-danger'>".$_SESSION["error"]."</p>";
unset($_SESSION["error"]);
}
?>
<form method="post" action="db/controller.php">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Registeration Form</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" placeholder="Name" class="form-control" />
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" placeholder="Email" class="form-control" />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" placeholder="Password" class="form-control" />
</div>
</div>
<div class="panel-footer">
<input type="submit" name="register" value="Registeration" class="btn btn-default">
New User <a href="login.php">Login</a>
</div>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
============================================================================
blog.php
<?php include 'header.php'; ?>
<?php 
if (isset($_REQUEST["id"])) {
$sql="select * from blogs where id='".$_REQUEST["id"]."'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);
}
$users=show_user($con,$row["id"]);
 ?>
<div class="container">
<div class="row">
<div class="col-md-4">
<img class="img-thumbnail" src="img/<?php echo $row["image"]; ?>">
</div>
<div class="col-md-8">
<h1><?php echo $row["title"]; ?></h1>
<small><?php echo $row["posted_at"]; ?> , <q><?php echo $users["name"] ?></q></small>
<hr>
<p><?php echo $row["description"]; ?></p>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
======================================================================

Create an Folder with name of db and inside db folder create two file

1. config.php
<?php 
session_start();
$con=mysqli_connect("localhost","root","","new_php_crud");
if (!$con) {
echo "<script>alert('Database Connection Failed')</script>";
}
 ?>

2. controller.php

<?php 
include 'config.php';
if (isset($_REQUEST["register"])) {
$name=$_REQUEST["name"];
$email=$_REQUEST["email"];
$password=$_REQUEST["password"];

$sql="INSERT INTO `users`(`name`, `email`, `password`) VALUES ('".$name."','".$email."','".$password."')";
if (mysqli_query($con,$sql)) {
$_SESSION["success"]="Registration Successfully";
$_SESSION["userid"]=$email;
header("Location:../admin/index.php");
}
else
{
$_SESSION["error"]="Registration Failed";
header("Location:register.php");
}
}
if (isset($_REQUEST["login"])) {
$email=$_REQUEST["email"];
$password=$_REQUEST["password"];

$sql="select * from users where email='".$email."' and password='".$password."'";
$res=mysqli_query($con,$sql);
if(mysqli_fetch_assoc($res))
{
$_SESSION["success"]="Login Successfully";
$_SESSION["userid"]=$email;
header("Location:../admin/index.php");
}
else
{
$_SESSION["error"]="Wrong Details Provided Failed";
header("Location:login.php");
}
}
if (isset($_REQUEST["blog_post"])) {
$title=addslashes($_REQUEST["title"]);
$description=addslashes($_REQUEST["description"]);
$img_file=$_FILES["image"]["name"];

$temp_img='noimg.jpg';
$folder='../img/';
if(move_uploaded_file($_FILES["image"]["tmp_name"], $folder.$img_file))
{
$temp_img=$img_file;
}
$sql="INSERT INTO `blogs`(`title`, `description`, `image`, `userid`) VALUES ('".$title."','".$description."','".$temp_img."','".$_REQUEST["userid"]."')";
if (mysqli_query($con,$sql)) {
$_SESSION["success"]="Posted Successfully";
header("Location:../admin/index.php");
}
else
{
$_SESSION["error"]="Post Failed";
header("Location:../admin/index.php");
}
}
if (isset($_REQUEST["blog_post_update"])) {
$id=$_REQUEST["id"];
$title=addslashes($_REQUEST["title"]);
$description=addslashes($_REQUEST["description"]);
$img_file=$_FILES["image"]["name"];
$temp_img=$_REQUEST["pre_img"];
$folder='../img/';
if(move_uploaded_file($_FILES["image"]["tmp_name"], $folder.$img_file))
{
$temp_img=$img_file;
}
$sql="UPDATE `blogs` set `title`='".$title."', `description`='".$description."', `image`='".$temp_img."' where id='".$id."'";
if (mysqli_query($con,$sql)) {
$_SESSION["success"]="Posted Updated Successfully";
header("Location:../admin/index.php");
}
else
{
$_SESSION["error"]="Post Updation Failed";
header("Location:../admin/index.php");
}
}
if (isset($_REQUEST["did"])) {
$sql="delete from blogs where id='".$_REQUEST["did"]."'";
if (mysqli_query($con,$sql)) {
$_SESSION["success"]="Post Deleted Successfully";
header("Location:../admin/index.php");
}
else
{
$_SESSION["error"]="Post Deletation Failed";
header("Location:../admin/index.php");
}
}
 ?>
===========================================================
Create an folder with img name
==============================================================
Create an folder with name of admin and inside this folder create following file:

index.php
<?php include 'header.php'; ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php 
if (isset($_SESSION["success"])) {
echo "<p class='alert alert-success'>".$_SESSION["success"]."</p>";
unset($_SESSION["success"]);
}
if (isset($_SESSION["error"])) {
echo "<p class='alert alert-danger'>".$_SESSION["error"]."</p>";
unset($_SESSION["error"]);
}
?>
<table class="table">
<thead>
<tr>
<th>Sno</th>
<th>Title</th>
<th>Description</th>
<th>Image</th>
<th>Date</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php 
$sql="select  * from blogs where userid='".$row["id"]."'";
$result=mysqli_query($con,$sql);
$sno=1;
while ($rows=mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $sno++; ?></td>
<td><?php echo $rows["title"]; ?></td>
<td><?php echo $rows["description"]; ?></td>
<td><img src="../img/<?php echo $rows["image"]; ?>" alt='<?php echo $rows["title"] ?>' height='50' width='50'></td>
<td><?php echo $rows["posted_at"]; ?></td>
<td><?php echo  "<a href='edit.php?id=".$rows["id"]."'>Edit</a>";?> <?php echo  "<a href='../db/controller.php?did=".$rows["id"]."'>Delete</a>";?> </td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
============================================

new-blog.php

<?php include 'header.php'; ?>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form method="post" action="../db/controller.php" enctype="multipart/form-data">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Blog Post Form</h3>
</div>
<div class="panel-body">
<input type="hidden" name="userid" value="<?php echo $row["id"] ?>">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" placeholder="Title of Blog" class="form-control" />
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" class="form-control" />
</div>
<div class="form-group">
<label>Description</label>
<textarea  name="description" placeholder="Description" class="form-control" rows="5"></textarea>
</div>
</div>
<div class="panel-footer">
<input type="submit" name="blog_post" value="Post Blog" class="btn btn-default">
</div>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>



==============================
header.php
<?php include '../db/config.php'; 
if (@$_SESSION["userid"]=='') {
$_SESSION["error"]="You Must Login First";
header("Location:../login.php");
}
else
{
$sql="select * from users where email='".$_SESSION["userid"]."'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($res);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container" style="margin-top: 80px;">
<div class="row">
<div class="col-md-12">

<nav class="navbar  bg-info">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="index.php"><?php echo $row["name"] ?> Panel</a>
    </div>
    <ul class="nav navbar-nav">
        <li><a href="index.php">Blogs</a></li>
<li><a href="new-blog.php">New Blog</a></li>
<li><a href="logout.php">Logout</a></li>
    </ul>
  </div>
</nav>
</div>
</div>
</div>

=====================\
footer.php
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>
============================
edit.php
<?php include 'header.php'; ?>
<?php 
if (isset($_REQUEST["id"])) {
$sql="select * from blogs where id='".$_REQUEST["id"]."'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);
}
 ?>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form method="post" enctype="multipart/form-data" action="../db/controller.php">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Edit Blog <?php echo $row["title"]; ?></h3>
</div>
<div class="panel-body">
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row["id"] ?>"/>
<label>Title</label>
<input type="text" name="title" placeholder="Name" class="form-control" value="<?php echo $row["title"]; ?>" />
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" class="form-control" />
<img src="../img/<?php echo $row["image"]; ?>" height='50' width='50'>
<input type="hidden" name="pre_img" value="<?php echo $row["image"]; ?>">
</div>
<div class="form-group">
<label>Description</label>
<textarea  name="description" placeholder="Description" class="form-control" rows="5"><?php echo $row["description"]; ?></textarea>
</div>
</div>
<div class="panel-footer">
<input type="submit" name="blog_post_update" value="Update Blog" class="btn btn-default">
</div>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
========================================================
Logout.php
<?php include 'header.php'; ?>
<?php 
$_SESSION["userid"]=="";
$_SESSION["success"]="successfully Logout";
header("Location:../login.php");
 ?>
<?php include 'footer.php'; ?>
=======================================================
Hence these are some basic process to install PHP Crud App in your system.


Thanks