我们可以在 JQuery 中执行 SQL 查询吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4234056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we Execute SQL Queries in JQuery
提问by Moon
can we execute mySQL queries in jQuery Calllback functions & misc. functions
我们可以在 jQuery 回调函数和杂项中执行 mySQL 查询吗?职能
like simple query
像简单的查询
UPDATE EMPLOYEE SET PAY = PAY + 500 WHERE E_ID = '32'
回答by
While you could do this using a callback to a server-side script to execute your queries against MySQL, it would be a quick way to security holes.
虽然您可以使用服务器端脚本的回调来执行针对 MySQL 的查询,但这将是一种快速消除安全漏洞的方法。
Generating SQL queries from anything run on an end user device is a terribly bad idea.
Never trust anything a user sends you.
从最终用户设备上运行的任何内容生成 SQL 查询是一个非常糟糕的主意。
永远不要相信用户发送给你的任何东西。
回答by rahul
The best thing you can do is to issue an AJAX request to a server and then execute this query using a server side code.
您能做的最好的事情是向服务器发出 AJAX 请求,然后使用服务器端代码执行此查询。
You can use jQuery.post()in this case.
在这种情况下,您可以使用jQuery.post()。
Edit
编辑
To get an overview of AJAX Read this
要获得 AJAX 的概述,请阅读此内容
Read thisto get and overview of AJAX methods in jQuery.
阅读本文以获取和概述 jQuery 中的 AJAX 方法。
Write a server side logic to execute the SQL command, in a page. Then using AJAX issue a request to that page.
在页面中编写服务器端逻辑来执行 SQL 命令。然后使用 AJAX 向该页面发出请求。
Sample Code
示例代码
$(function(){
// Bind a click event to your anchor with id `updateSal`
$("#updateSal").click(function(){
// get your employeeID
var empID = "32";
// issue an AJAX request with HTTP post to your server side page.
//Here I used an aspx page in which the update login is written
$.post("test.aspx", { EmpID: empID},
function(data){
// callack function gets executed
alert("Return data" + data);
});
// to prevent the default action
return false;
});
});
回答by vin val
Try this:
尝试这个:
https://github.com/vinval/MyJSQL
https://github.com/vinval/MyJSQL
How to:
如何:
$.jsql({
tbName:"EMPLOYEE",
set:["PAY=PAY+500"],
where:"E_ID=32"
}, function(){
//this is the callback
})
回答by Adam Flanagan
You can't directly access a database from javascript/jQuery but you can post to a web service on the server which can access the database.
您不能直接从 javascript/jQuery 访问数据库,但您可以发布到可以访问数据库的服务器上的 Web 服务。