使用 javascript/AJAX 将数据插入 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4139532/
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
Insert data into MySQL Database using javascript/AJAX
提问by Pow
I have made a very simple page using google maps API with several fields where users are going to put some data. It looks like following -
我使用谷歌地图 API 制作了一个非常简单的页面,其中有几个字段,用户将在其中放置一些数据。看起来像以下 -
http://aiworker2.usask.ca/marker_field_db.html
http://aiworker2.usask.ca/marker_field_db.html
What I want to do is store the data into MySQL database using javascript/Ajax. I have found several examples that has used Jquery. I'm very new to this javascript/Ajax/Jquery platform. Now my question is-
我想要做的是使用 javascript/Ajax 将数据存储到 MySQL 数据库中。我发现了几个使用 Jquery 的例子。我对这个 javascript/Ajax/Jquery 平台很陌生。现在我的问题是——
- Is it possible to insert data into MySQL database without using JQuery?
- Can anyone send any link of simple example or tutorial to deal with the issue?
- 是否可以在不使用 JQuery 的情况下将数据插入到 MySQL 数据库中?
- 任何人都可以发送任何简单示例或教程的链接来处理这个问题吗?
Thanks in advance.
提前致谢。
回答by Rob Sobers
Your JavaScript runs on the client (in the browser). Your MySQL database exists on a server.
您的 JavaScript 在客户端(在浏览器中)上运行。您的 MySQL 数据库存在于服务器上。
In short, the client-side JavaScript cannot establish a directconnection to MySQL. You need to make an AJAX request to the server which runs a script that interacts with MySQL. The script can be written in any language that has a MySQL library.
简而言之,客户端 JavaScript 无法与 MySQL建立直接连接。您需要向运行与 MySQL 交互的脚本的服务器发出 AJAX 请求。该脚本可以用任何具有 MySQL 库的语言编写。
Here's an example where an AJAX request is made, which calls a PHP script on the server, which, in turn, grabs data from a MySQL database and returns results back to the client:
下面是一个发出 AJAX 请求的示例,它调用服务器上的 PHP 脚本,然后从 MySQL 数据库中获取数据并将结果返回给客户端:
回答by Ashvin
You can connect to a MySQL database via JavaScript using ActivexObject. Example:
您可以使用 ActivexObject 通过 JavaScript 连接到 MySQL 数据库。例子:
var conn = new ActiveXObject("ADODB.Connection");
var connStr = "Provider=SQLOLEDB;Server=mysite;Database=Northwind;User Id=MyId;Password=123aBc";
conn.open(connStr);
回答by KeatsKelleher
You could send a GET request to a server script with AJAX:
您可以使用 AJAX 向服务器脚本发送 GET 请求:
回答by Felix Kling
Is it possible to insert data into MySQL database without using JQuery?
是否可以在不使用 JQuery 的情况下将数据插入到 MySQL 数据库中?
Yes.
That said, jQuery helps you a lot dealing with Ajax (and cross-browser compatibility), so I would still consider it.
是的。
也就是说,jQuery 可以帮助您处理 Ajax(和跨浏览器兼容性),所以我仍然会考虑它。
Can anyone send any link of simple example or tutorial to deal with the issue?
任何人都可以发送任何简单示例或教程的链接来处理这个问题吗?
Any Ajax tutorial will do it (e.g. https://developer.mozilla.org/en/AJAX). You just have to learn how to send data to the server side (as POST
or GET
request). Then it is just PHP and MySQL.
任何 Ajax 教程都可以做到(例如https://developer.mozilla.org/en/AJAX)。您只需要学习如何将数据发送到服务器端(作为POST
或GET
请求)。那么它只是PHP和MySQL。