我有一个可以单击的编辑按钮,该按钮将允许表格单元格变得可编辑。它接受数字输入。但是,您可以在下面看到,如果输入不满足 update-index.php
表中的 sql 语句中的任何条件,则应该出现某种错误,否则我应该得到一个我的控制台中显示消息:该行无法插入/更新
。
因此,例如,如果我在表格单元格中输入 1517,该值应该产生 The row failed to insert/update
消息,但它不会插入或更新,而这正是我想要的想要,但是它在我的日志中给了我这条消息:行插入/更新成功
。
我怎样才能得到这个给我日志中发生的事情的正确消息,以便我可以正确地向用户显示消息,这样他们就不会认为它已成功输入,而实际上并非如此?
JavaScript:
// ----- 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);
}
}
});
更新索引.php
$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);
?>
最佳答案
问题是,只有当 sql 出现异常时,才显示失败消息。仅仅因为该行未插入并不意味着该语句无效。
Returns TRUE on success or FALSE on failure.
http://php.net/manual/en/pdostatement.execute.php
因此您的 sql 执行成功并且不执行任何操作,因此 ajax 调用的结果将为 true。
如果您想知道您的语句是否实际修改了任何数据,您可以使用以下内容:
echo json_encode($stmt->rowCount() > 0)
关于javascript - 在 PHP 脚本中得到正确的结果,但在 console.log() 中得到错误的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42581040/