【问题标题】:ajax's success function not returning all rows from databaseajax的成功函数不返回数据库中的所有行
【发布时间】:2019-10-11 16:16:56
【问题描述】:

我在 ajax 中使用了成功函数,一切都很顺利,直到我在我的数据库表中添加了一个新行并且成功函数没有返回该行只是出于某种原因,即性别 ID,这是我的 ajax 函数:

function editProduct(productId = null) {
  if (productId) {
    $("#productId").remove();
    // remove text-error
    $(".text-danger").remove();
    // remove from-group error
    $(".form-group")
      .removeClass("has-error")
      .removeClass("has-success");
    // modal spinner
    $(".div-loading").removeClass("div-hide");
    // modal div
    $(".div-result").addClass("div-hide");

    $.ajax({
      url: "php_action/fetchSelectedProduct.php",
      type: "post",
      data: { productId: productId },
      dataType: "json",
      success: function(response) {
        // alert(response.product_image);
        // modal spinner
        $(".div-loading").addClass("div-hide");
        // modal div
        $(".div-result").removeClass("div-hide");

        $("#getProductImage").attr("src", "stock/" + response.product_image);

        $("#editProductImage").fileinput({});

        // product id
        $(".editProductFooter").append(
          '<input type="hidden" name="productId" id="productId" value="' +
            response.product_id +
            '" />'
        );
        $(".editProductPhotoFooter").append(
          '<input type="hidden" name="productId" id="productId" value="' +
            response.product_id +
            '" />'
        );

        // product name
        $("#editProductName").val(response.product_name);
        // quantity
        $("#editQuantity").val(response.quantity);
        // rate
        $("#editRate").val(response.price);
        // brand name
        $("#editBrandName").val(response.brand_id);
        // category name
        $("#editCategoryName").val(response.categories_id);

        $("#editGender").val(response.gender_id);

        // status
        $("#editProductStatus").val(response.active);

        // update the product data function
        $("#editProductForm")
          .unbind("submit")
          .bind("submit", function() {
            // form validation
            var productImage = $("#editProductImage").val();
            var productName = $("#editProductName").val();
            var quantity = $("#editQuantity").val();
            var rate = $("#editRate").val();
            var brandName = $("#editBrandName").val();
            var categoryName = $("#editCategoryName").val();
            var genderName = $("#editGender").val();
            var productStatus = $("#editProductStatus").val();

            if (productName == "") {
              $("#editProductName").after(
                '<p class="text-danger">Product Name field is required</p>'
              );
              $("#editProductName")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editProductName")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editProductName")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (quantity == "") {
              $("#editQuantity").after(
                '<p class="text-danger">Quantity field is required</p>'
              );
              $("#editQuantity")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editQuantity")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editQuantity")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (rate == "") {
              $("#editRate").after(
                '<p class="text-danger">Rate field is required</p>'
              );
              $("#editRate")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editRate")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editRate")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (brandName == "") {
              $("#editBrandName").after(
                '<p class="text-danger">Brand Name field is required</p>'
              );
              $("#editBrandName")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editBrandName")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editBrandName")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (categoryName == "") {
              $("#editCategoryName").after(
                '<p class="text-danger">Category Name field is required</p>'
              );
              $("#editCategoryName")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editCategoryName")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editCategoryName")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (genderName == "") {
              $("#editGender").after(
                '<p class="text-danger">Category Name field is required</p>'
              );
              $("#editGender")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editGender")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editGender")
                .closest(".form-group")
                .addClass("has-success");
            }

            if (productStatus == "") {
              $("#editProductStatus").after(
                '<p class="text-danger">Product Status field is required</p>'
              );
              $("#editProductStatus")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editProductStatus")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editProductStatus")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (
              productName &&
              quantity &&
              rate &&
              brandName &&
              genderName &&
              categoryName &&
              productStatus
            ) {
              // submit loading button
              $("#editProductBtn").button("loading");

              var form = $(this);
              var formData = new FormData(this);

              $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: formData,
                dataType: "json",
                cache: false,
                contentType: false,
                processData: false,
                success: function(response) {
                  console.log(response);
                  if (response.success == true) {
                    // submit loading button
                    $("#editProductBtn").button("reset");

                    $(
                      "html, body, div.modal, div.modal-content, div.modal-body"
                    ).animate({ scrollTop: "0" }, 100);

                    // shows a successful message after operation
                    $("#edit-product-messages").html(
                      '<div class="alert alert-success">' +
                        '<button type="button" class="close" data-dismiss="alert">&times;</button>' +
                        '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> ' +
                        response.messages +
                        "</div>"
                    );

                    // remove the mesages
                    $(".alert-success")
                      .delay(500)
                      .show(10, function() {
                        $(this)
                          .delay(3000)
                          .hide(10, function() {
                            $(this).remove();
                          });
                      }); // /.alert

                    // reload the manage student table
                    manageProductTable.ajax.reload(null, true);

                    // remove text-error
                    $(".text-danger").remove();
                    // remove from-group error
                    $(".form-group")
                      .removeClass("has-error")
                      .removeClass("has-success");
                  } // /if response.success
                }
                // /success function
              }); // /ajax function
            } // /if validation is ok

            return false;
          }); // update the product data function

        // update the product image
        $("#updateProductImageForm")
          .unbind("submit")
          .bind("submit", function() {
            // form validation
            var productImage = $("#editProductImage").val();

            if (productImage == "") {
              $("#editProductImage")
                .closest(".center-block")
                .after(
                  '<p class="text-danger">Product Image field is required</p>'
                );
              $("#editProductImage")
                .closest(".form-group")
                .addClass("has-error");
            } else {
              // remov error text field
              $("#editProductImage")
                .find(".text-danger")
                .remove();
              // success out for form
              $("#editProductImage")
                .closest(".form-group")
                .addClass("has-success");
            } // /else

            if (productImage) {
              // submit loading button
              $("#editProductImageBtn").button("loading");

              var form = $(this);
              var formData = new FormData(this);

              $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: formData,
                dataType: "json",
                cache: false,
                contentType: false,
                processData: false,
                success: function(response) {
                  if (response.success == true) {
                    // submit loading button
                    $("#editProductImageBtn").button("reset");

                    $(
                      "html, body, div.modal, div.modal-content, div.modal-body"
                    ).animate({ scrollTop: "0" }, 100);

                    // shows a successful message after operation
                    $("#edit-productPhoto-messages").html(
                      '<div class="alert alert-success">' +
                        '<button type="button" class="close" data-dismiss="alert">&times;</button>' +
                        '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> ' +
                        response.messages +
                        "</div>"
                    );

                    // remove the mesages
                    $(".alert-success")
                      .delay(500)
                      .show(10, function() {
                        $(this)
                          .delay(3000)
                          .hide(10, function() {
                            $(this).remove();
                          });
                      }); // /.alert

                    // reload the manage student table
                    manageProductTable.ajax.reload(null, true);

                    $(".fileinput-remove-button").click();

                    $.ajax({
                      url: "php_action/fetchProductImageUrl.php?i=" + productId,
                      type: "post",
                      success: function(response) {
                        $("#getProductImage").attr("src", response);
                      }
                    });

                    // remove text-error
                    $(".text-danger").remove();
                    // remove from-group error
                    $(".form-group")
                      .removeClass("has-error")
                      .removeClass("has-success");
                  } // /if response.success
                } // /success function
              }); // /ajax function
            } // /if validation is ok

            return false;
          }); // /update the product image
      } // /success function
    }); // /ajax to fetch product image
  } else {
    alert("error please refresh the page");
  }
} // /edit product function

,这是我用来从数据库中获取数据的 sql 查询 fetchSelectedProduct:

<?php   

require_once 'core.php';

$productId = $_POST['productId'];

$sql = "SELECT product_id, product_name, product_image, brand_id, categories_id, quantity, price, active, status, gender_id FROM product WHERE product_id = $productId";
$result = $connect->query($sql);

if($result->num_rows > 0) { 
 $row = $result->fetch_array();
} // if num_rows

$connect->close();

echo json_encode($row);

有人知道我的代码有什么问题吗?我试图修复它,但我不能..

【问题讨论】:

  • 我尝试调试的最简单方法是打开 phpmyadmin(如果您使用一个),然后将 SQL 查询放入SQL 选项卡中,用一些随机 ID 替换变量。这通常会非常准确地显示您的错误实际在哪里,也许是错字或其他什么。顺便提一下,你的代码不安全,使用准备好的语句而不是带参数的直接查询。
  • @Danielius 我试过了,一切都很顺利,我会将我的查询更改为准备好的语句。谢谢。
  • 请删除与接收和处理数据/行没有直接关系的任何内容。这是太多不必要的代码,有 4 个 $.ajax() 调用 - How to create a Minimal, Reproducible Example

标签: php jquery ajax


【解决方案1】:

几乎可以肯定是 ajax 缓存所以只需添加:

cache: false

到您的呼叫,或向请求 uri 添加新参数:

'?t=' + Date.now();

【讨论】:

  • “几乎可以肯定...” - 请求是posts,它们没有被缓存或者已经有cache: false
  • 是的,not cacheable, unless the response includes appropriate Cache-Control or Expires header fields... 这不是一次,而是在 IE 和 iOS 上发生了几次——他们只是缓存了它。虽然在 Firefox 中一切正常,但在 Mac 和 iOS 上使用 Safari 的人却有很多抱怨。
猜你喜欢
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多