我有一个可以单击的编辑按钮,这将允许表格单元格可编辑。它接受数字输入。但是,您可以在下面看到,如果输入不符合我的The row failed to insert/update
表中我的sql语句中的任何条件,那么应该存在某种错误,或者我应该在我的控制台中收到一条消息{{ 1}}。
因此,例如,如果我在表格单元格中输入1517,这是一个应该生成Row inserted/updated successfully
消息的值,它不会插入或更新我想要的内容,但是它会给我这个消息我的日志:// ----- Edit Row -----
$(document).on("click", "#skuTable .edit", function () {
var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function () {
return $(this).find('.edit').length === 0;
});
if ($this.val() === 'Edit') {
$this.val('Save');
if($this.id !== '.major_cat') {
tds.not('.major_cat').not('.minor_cat').not('.rep_code').not('.sku_desc').not('.sku_status').not('.create_date').not('.sku').not('.sku_group').prop('contenteditable', true);
}
} else {
var isValid = true;
var errors = '';
$('#myDialogBox').empty();
var elements = tds;
if (tds.find('input').length > 0) {
elements = tds.find('input');
}
var dict = {};
elements.each(function (index, element) {
var type = $(this).attr('class');
var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text();
console.log(type);
// ----- Switch statement that provides validation for each table cell -----
switch (type) {
case "sku":
dict["SKU"] = value;
break;
case "group_id":
if (!$.isNumeric(value)) {
isValid = false;
errors += "Please enter a numeric Group ID\n";
}
if (isValid) {
dict["Group_ID"] = value.trim();
}
break;
/*case "sku_group":
dict["SKU Group"] = value.trim();
break;*/
}
})
if (isValid) {
console.log(dict);
$this.val('Edit');
tds.prop('contenteditable', false);
if(confirm("Saving will insert or update the data. Do you wish to continue?") == true) {
var request = $.ajax({
type: "POST",
url: "update-index.php",
data: dict
});
request.done(function (response, textStatus, jqXHR){
if(JSON.parse(response) == true){
console.log("row inserted/updated");
alert("Row inserted/updated successfully");
} else {
console.log("row failed to updated");
alert("The row failed to insert/update");
console.log(response);
console.log(textStatus);
console.log(jqXHR);
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.log(textStatus);
console.log(jqXHR);
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
});
} else {
alert("The entry was not saved");
} // ends if statement with confirmation box
} else {
alert(errors);
}
}
});
。
我如何才能得到正确的消息,告诉我日志中发生的事情,以便我能正确地向用户显示消息,这样他们就不会认为它已成功输入,而实际上并不是这样。
JavaScript的:
$Group_ID = $_POST['Group_ID'];
$SKU = $_POST['SKU'];
$host="xxxxxxxxx";
$dbName="xxxxxx";
$dbUser="xxxxxxxxxxxxxx";
$dbPass="xxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$sql = "IF EXISTS (SELECT SKU FROM SKU_Group_Index WHERE SKU = ?)
AND EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
BEGIN
UPDATE SKU_Group_Index
SET Group_ID = ?
WHERE SKU = $SKU
END
ELSE
BEGIN
IF EXISTS (SELECT Group_ID FROM SKU_Group_Dim WHERE Group_ID = ?)
BEGIN
INSERT INTO SKU_Group_Index (SKU, Group_ID) VALUES (?, ?)
END
END";
$stmt = $pdo->prepare($sql);
$result = $stmt->execute([$SKU, $Group_ID, $Group_ID, $Group_ID, $SKU, $Group_ID]);
echo json_encode($result);
?>
更新的index.php
<form method="post" class="blog-form" action="/blog/post" enctype="multipart/form-data">
答案 0 :(得分:1)
问题是当sql出现异常时,您只显示失败的消息。仅因为未插入行并不意味着该语句无效。
成功时返回 TRUE ,失败时返回 FALSE 。
http://php.net/manual/en/pdostatement.execute.php
所以你的sql成功执行并且没有做任何事情,所以你的ajax调用的结果将是真的。
如果您想知道您的陈述是否实际修改了任何数据,您可以使用以下内容:
echo json_encode($stmt->rowCount() > 0)