【发布时间】: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">×</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">×</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