简体   繁体   中英

Database not updating using php and jquery

Basically, I want the status to change based on the id. There's no error but it is not updating.

index.php

$(document).ready(function() {
      $('.seatCharts-seat').click(function(){ 
       var id = jQuery(this).attr("id");          
        
        $seatid = id;

        $.post("class/booking.php",
        {
          seatid:$seatid
        },
        function(data, status){
          console.log('data',data);
          console.log('status',status);
          console.log($seatid);

        });
      });
  });

booking.php

<?php
require_once 'config.php';

class booking extends config{
    public $id;

    public function __construct($id){
        $this->id = $id;
    }

    public function editTask(){
        $con = $this->con();
        $sql = "UPDATE `tbl_booking` SET `status`= 'unavailable' WHERE `id` = $this->id";
        $data = $con->prepare($sql);

        if($data->execute()){
            return true;
        }else{
            return false;
        }
    }
}
?>

Option 1 is to create a common file to handle all ajax related activities for the project. Say I would call it as myAjaxHandler.php

Then I would include the required class files are creates its instances to get the required methods called. Keep in mind when your projects get bigger it would be a bit difficult to maintain.

myAjaxHandler.php

<?php
require_once 'booking.php';

if(!empty($_POST) && isset($_POST['seatid'])) {
    $booking = new booking($_POST['seatid']);
    $result = $booking->editTask();
    echo $result; // Would get 1 for true & 0 for false
}

Changes to ajax

$.post("path/to/myAjaxHandler.php", 
    { 
       seatid: $seatid 
    }, 
    function(data, status){
      console.log('data',data);
      console.log('status',status);
    });

Option 2 is to have the above if block inside the booking.php file itself.

<?php
require_once 'config.php';

if(!empty($_POST) && isset($_POST['seatid'])) {
    $booking = new booking($_POST['seatid']);
    $result = $booking->editTask();
    echo $result; // Would get 1 for true & 0 for false
}

class booking extends config { ... }

//Ajax
$.post("path/to/booking.php", 
  { 
   seatid: $seatid 
  }, 
  function(data, status){
    console.log('data',data);
    console.log('status',status);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM