javaScript-AJAX请求中的SQL语句
我只使用JavaScript编程,现在我想使用一些数据库并学习SQL。我想知道在AJAX请求中发送SQL语句是否是一种常见且安全的态度,例如:javaScript-AJAX请求中的SQL语句,javascript,sql-server,ajax,Javascript,Sql Server,Ajax,我只使用JavaScript编程,现在我想使用一些数据库并学习SQL。我想知道在AJAX请求中发送SQL语句是否是一种常见且安全的态度,例如: xhttp= new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { //something } }; var sql = 'SELECT * FROM
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//something
}
};
var sql = 'SELECT * FROM Customers'; //creating SQL statements in JS like this
xhttp.open("GET", "getFromDatabase.php?q="+sql, true);
xhttp.send();
//PHP:
$result = mysqli_query($con,$q);
echo $result;
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//something
}
};
xhttp.open("GET", "getFromDatabase.php?q=getCustomers", true);
xhttp.send();
//PHP:
if ($q==="getCustomers") {
$sql="SELECT * FROM Customers"; //here are ALL SQL statements
$result = mysqli_query($con,$sql);
echo $result;
}
在AJAX open()中指示的php文件中保留并生成所有必要的SQL语句,而不是像这样:
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//something
}
};
var sql = 'SELECT * FROM Customers'; //creating SQL statements in JS like this
xhttp.open("GET", "getFromDatabase.php?q="+sql, true);
xhttp.send();
//PHP:
$result = mysqli_query($con,$q);
echo $result;
xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//something
}
};
xhttp.open("GET", "getFromDatabase.php?q=getCustomers", true);
xhttp.send();
//PHP:
if ($q==="getCustomers") {
$sql="SELECT * FROM Customers"; //here are ALL SQL statements
$result = mysqli_query($con,$sql);
echo $result;
}
我在互联网上读到的所有教程和示例都喜欢第二个attitube,但对于我来说——我不懂PHP,现在也不想学习——第一个(JS)态度会更简单,只是为了学习和测试如何处理数据库。然后,我可以创建一个简单的php文件,用于返回JSON和一些数据以及JavaScript中的所有其他内容。。。我建议你使用旗子。。。在ajax请求中发送不同的标志,以便在php中执行不同的查询。。。php脚本将获取标志并决定运行哪个查询 答案是“不!”。请不要那样做。这会为SQL注入攻击和其他不利于安全的事情打开数据库 如果您想学习如何使用SQL,请使用PHP、Node.js、Python或其他易于学习的语言编写RESTful服务,并在后端运行SQL
在未正确参数化的情况下,切勿将字符串从UI直接传递到数据库。这就是整个数据库被盗/删除的原因。。。不要这样做。。。他们需要在php中。。。。如果没有人可以运行SQL请求。要么被盗,要么有人打开控制台并使用语句执行请求
DROP TABLE Customers
。鉴于用户可以访问的工具,恶意人员将能够修改和利用此类代码,您的数据可能会泄漏。这比OP的想法要好得多,在生产环境中运行了200或300个查询之后,仍然需要进行维护。