提问者:小点点

没有使用php和jquery更新数据库


基本上,我想根据ID更改状态。没有错误,但它没有更新。

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;
        }
    }
}
?>

共1个答案

匿名用户

选项1是创建一个公共文件来处理项目的所有ajax相关活动。假设我将其称为myajaxhandler.php

然后,我将包括所需的类文件,创建它的实例,以获得所需的方法调用。请记住,当您的项目变得更大时,维护起来会有点困难。

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
}

对ajax的更改

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

选项2是在booking.php文件本身中具有上面的if块。

<?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);
  });