PHP ve MYSQL’de Otomatik Kaydetme Örneği.

PHP ve MYSQL'de Otomatik Kaydetme Örneği.
Aşağıdaki Tabloyu Oluşturun.
CREATE TABLE `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

config.php dosyasını oluşturun ve aşağıdaki kod satırını ekleyin.
$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());

index.php dosyasını oluşturun ve aşağıdaki kod satırını ekleyin.

<html lang="en">
<head>
  <title>Auto Save Example In PHP</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">  
  <!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>     
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
</head>
<body>
<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>
                    </div>                    
                    <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>
                        </div>
                </div>
                <input type="hidden" id='postid' value="" >         
              
                <input type="button" id='submit' class="next btn btn-success" style="margin-left:30%" value="Submit">
            </div>          
        </form>
      </div>
    </div>
</div> 
</body> 
<head>
<script>
setInterval(saveData, 5000);
function saveData(){
 
 var postid = $('#postid').val();
 var title = $('#title').val().trim();
 var content = $('#description').val().trim();

 if(title != '' || content != ''){
  
  $.ajax({
   url: 'autosave.php',
   type: 'post',
   data: {postid:postid,title:title,content:content},
   success: function(response){
    $('#postid').val(response);
   } 
  });
 }
 
}

</script>
</head>
</html>

autosave.php dosyası oluşturun ve kod satırının altına ekleyin.

<?php
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);       
  }  
  else  
  {  
    //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;
  }
 }  


Bir cevap yazın

E-posta hesabınız yayımlanmayacak.