SQL更新无法与AJAX一起使用

时间:2019-04-05 03:12:42

标签: php sql ajax

每个循环都有一个带有href的循环,具体取决于int值:

<td data-target="scheduled">
    <input id='userId' type='hidden'/>

    <?php
if ($rowOrder['scheduled'] == 1) {
?>

    <a href="#" data-role="update" data-id="<?php echo $rowOrder['orderid'] ;?>"><button class="btn-success">YES</button></a>

    <?php
} else if ($rowOrder['scheduled'] == 0) {
?>
    <a href="#" data-role="update" data-id="<?php echo $rowOrder['orderid'] ;?>"><button class="btn-danger">NO</button></a>

    <?php
} else if ($rowOrder['scheduled'] == 2) {
?>
    <a href="#" data-role="update" data-id="<?php echo $rowOrder['orderid'] ;?>"><button class="btn-warning">CANCELLED</button></a>

    <?php
}
?>
</td>

我可以使用以下模式更改它:(是,否,已取消)

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content" style="width: 300px; margin: 0 auto;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <a href="#" id="update_yes" class="btn btn-success text-center center-block">YES</a><br>
          <a href="#" id="update_no" class="btn btn-danger text-center center-block">NO</a><br>
          <a href="#" id="update_cancelled" class="btn btn-warning text-center center-block">CANCEL</a>
        </div>
      </div>
    </div>
  </div>
</div>

此AJAX代码依次使用模式:

<script>
    $(document).ready(function(){

   //append values in input fields
            $(document).on('click','a[data-role=update]',function(){
                  var id  = $(this).data('id');
                  var scheduled  = $('#'+id).children('td[data-target=scheduled]').text();

                  $('#scheduled').val(scheduled);
                  $('#userId').val(id);
                  $('#myModal').modal('toggle');
            });

            // now create event to get data from fields and update in database

            $('#update_no').click(function(){
               var id  = $('#userId').val();
               var scheduled = 0;

               $.ajax({
                   url      : 'orders-claimed.vc.php',
                   method   : 'post',
                   data     : {scheduled: scheduled , id: id},
                   success  : function(response){
                                 // now update user record in table
                                  $('#'+id).children('td[data-target=scheduled]').text('<a href="#"><button class="btn-danger">NO</button></a>');
                                  $('#myModal').modal('toggle');
                                  window.location.reload()
                              }
               });
            });

            $('#update_yes').click(function(){
               var id  = $('#userId').val();
               var scheduled = 1;

               $.ajax({
                   url      : 'orders-claimed.vc.php',
                   method   : 'post',
                   data     : {scheduled: scheduled , id: id},
                   success  : function(response){
                                 // now update user record in table
                                  $('#'+id).children('td[data-target=scheduled]').html('<a href="#"><button class="btn-success">YES</button></a>');
                                  $('#myModal').modal('toggle');
                                  window.location.reload()
                              }
               });
            });

            $('#update_cancelled').click(function(){
               var id  = $('#userId').val();
               var scheduled = 2;

               $.ajax({
                   url      : 'orders-claimed.vc.php',
                   method   : 'post',
                   data     : {scheduled: scheduled , id: id},
                   success  : function(response){
                                 // now update user record in table
                                  $('#'+id).children('td[data-target=scheduled]').text('<a href="#"><button class="btn-warning">CANCELLED</button></a>');
                                  $('#myModal').modal('toggle');
                                  window.location.reload()
                              }
               });
            });

       });
       </script>

现在我有一个单独的文件,我使用require_once()进行了链接: orders-claimed.vc.php

<?php
  /********** Headers **********/
  $routePath = "../";

  require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

  require_once($routePath . "_config/app.php");

  require_once($routePath . "_lib/c/Photo_Resizer.php");

  require_once($routePath . "_lib/c/slug.php");

  require_once($routePath . "_mc/Order.mc.php");
  $mcOrder = new Order_MC();

  // Check Session Credentials and auto-login
  if (isset($_SESSION['usrcmsid']) && isset($_SESSION['accesslevel']))
  {
    if ($_SESSION['accesslevel'] != 'CMS') {
      header('Location: signout.php'); die();
    }
  } else {
    header('Location: signout.php'); die();
  }

  // $pageMax = ceil($mcOrder->SelectOrders_Count($db, $orderid) / $limit);
  // Init page params
  $limit = 20;
  $status = isset($_GET['s']) ? $_GET['s'] : '';
  $pageNum = (isset($_GET['pagenum']) AND !empty($_GET['pagenum'])) ? $_GET['pagenum'] : 1;

  // Access database
  $lstOrder = $mcOrder->SelectOrder($db);

  if(isset($_POST['id'])){

      $orderid = $_POST['id'];
      $scheduled = $_POST['scheduled'];

      $stmt = $db->prepare('UPDATE order SET scheduled = :scheduled WHERE orderid = :orderid');
      $stmt->bindValue(':scheduled', $scheduled, PDO::PARAM_INT);
      $stmt->bindValue(':orderid', $orderid, PDO::PARAM_INT);
      $stmt->execute();
  }

?>

我担心的是,我认为更新问题不能很好地解决在if(isset($_POST['id'])) {代码内部。当前的一个似乎无法正常工作,我也尝试了以下操作,但无济于事:

$result  = mysqli_query($db , "UPDATE order SET scheduled = '$scheduled' WHERE orderid='$orderid'");

正在使用的变量为orderid(bigint20)和scheduled(int2)。 另外,我的数据库使用以下功能连接:

<?php
class config_db {
  public function init() {
    $db = new PDO(--MY DATABASE CONNECTION GOES HERE--);
    date_default_timezone_set('Hongkong');
    return $db;
  }
}
?>

enter image description here

更新:

即使我有一行:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

我得到了错误(在开发人员工具中):

enter image description here

0 个答案:

没有答案