Auto Save Example In PHP & MYSQL.

Auto Save Example In PHP & MYSQL.

In this post we’re going to learn how to save data automatically in database on specific time interval using Ajax Jquery with PHP and Mysql. This type of functionality you have seen into WordPress Admin side, any modern editor . However, also at Admin side when we’ve produce new post also after a specific interval of time it’ll save as draft our post or page automatically in Database, If you have used WordPress CMS. So our data will safe if we’re forget to publish our content and we come after some time also our content will be placed in Database as draft. So, Tthis type of functionality we’re going to learn into this post. In this post We’ll describe you a simple post illustration. We’ve simple form for posting simple composition with title and description.

When user enter title and description then after some time interval post automatically save into database table. This effects happens only after user enter post title and description. In this tutorial if post enter for first time also it fit into database table but if post formerly fitted also it’ll modernize that post data on regular time interval.

Step 1

Step 2 

Step 3 

Step 4 

Create Below Table.

CREATE TABLE `posts` (
`title` varchar(100) NOT NULL,
`description` text NOT NULL,

Create config.php file and add below line of code.

$host = “localhost”; /* Host name */
$user = “root”; /* User */
$password = “”; /* Password */
$dbname = “auto_save_demo”; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die(“Connection failed: ” . mysqli_connect_error());

Create index.php file and add below line of code.

<html lang=”en”>
<title>Auto Save Example In PHP</title>
<link rel=”stylesheet” href=””>
<!– <script src=””></script>
<link rel=”stylesheet” href=””> –>
<script src=””></script>
<script src=””></script>
<h3 class=”text-success” align=”center”>Auto Save Example In PHP</h3><br>
<div class=”container”>
<div class=”panel-group”>
<div class=”panel panel-primary”>
<div class=”panel-heading”>Auto Save Example In PHP</div>
<form class=”form-horizontal” method=”post”>
<div class=”panel-body”>
<div class=”form-group”>
<label class=”control-label col-sm-2″ for=”title”>Post Title:</label>
<div class=”col-sm-5″>
<input type=”text” class=”form-control” id=”title” name=”title” required>
<div class=”form-group”>
<label class=”control-label col-sm-2″ for=”description”>Description:</label>
<div class=”col-sm-5″>
<textarea class=”form-control” id=”description” cols=”5″ rows=”5″ name=”description”></textarea>
<input type=’hidden’ id=’postid’ value=” >

<input type=’button’ id=’submit’ class=”next btn btn-success” style=’margin-left:30%’ value=’Submit’>
setInterval(saveData, 5000);
function saveData(){

var postid = $(‘#postid’).val();
var title = $(‘#title’).val().trim();
var content = $(‘#description’).val().trim();

if(title != ” || content != ”){

url: ‘autosave.php’,
type: ‘post’,
data: {postid:postid,title:title,content:content},
success: function(response){



Create autosave.php file and add below line of code.

include “config.php”;
if(isset($_POST[“title”]) && isset($_POST[“content”]))
$post_id = mysqli_real_escape_string($con, $_POST[“postid”]);
$post_title = mysqli_real_escape_string($con, $_POST[“title”]);
$post_description = mysqli_real_escape_string($con, $_POST[“content”]);

if($post_id != ”)
//update post
$sql = “UPDATE posts SET title = ‘”.$post_title.”‘, description = ‘”.$post_description.”‘ WHERE id = ‘”.$_POST[“postid”].”‘”;
mysqli_query($con, $sql);
//insert post
$sql = “INSERT INTO posts(title,description) VALUES (‘”.$post_title.”‘, ‘”.$post_description.”‘)”;
mysqli_query($con, $sql);
$last_insert_id = mysqli_insert_id($con);
echo $last_insert_id;exit;


Upload Image In Angular With PHP                   Upload File In VueJs With PHP.

The post Auto Save Example In PHP & MYSQL. appeared first on PHPFOREVER.

Leave a Reply

Your email address will not be published.