简体   繁体   English

如何使用JavaScript将数据存储到mysql数据库中?

[英]How to store data into a mysql database using javascript?

I'm still pretty new to AJAX. 我对AJAX还是很陌生。 We have a MySQL database that stores addresses for people who register with the site. 我们有一个MySQL数据库,用于存储向该网站注册的人员的地址。 We then use those stored addresses to create markers on a Google Map (API v3) through a JSON query. 然后,我们使用这些存储的地址通过JSON查询在Google Map(API v3)上创建标记。 The problem is, that's pretty slow, and limited in the number of queries we're allowed. 问题是,这非常慢,并且我们允许的查询数量有限。 So we've created fields in the database to store the latitude and longitude that is being called by JSON. 因此,我们在数据库中创建了一些字段来存储JSON所调用的纬度和经度。

Through each pass of the loop, the numbers can be retrieved through 通过循环的每一遍,可以通过以下方式检索数字

result[0].geometry.location.lat() result[0].geometry.location.lng()

How do I take those numbers and store them into their empty fields in the database? 如何获取这些数字并将其存储到数据库中的空字段中?

Javascript runs on the client/browser side - its not a good idea to give that access to your mysql-server/databases. Javascript在客户端/浏览器端运行-让您的mysql-server / database访问权限不是一个好主意。

Just use some server-side logic (php) for that. 只需使用一些服务器端逻辑(php)。

Heres an example from W3 retrieving data via AJAX and PHP, just change to updating stuff 这是W3通过AJAX和PHP检索数据的示例,只是更改为更新内容

http://www.w3schools.com/php/php_ajax_database.asp http://www.w3schools.com/php/php_ajax_database.asp

You can send data to a server with javascript, but you need a library to interact with a database system, so on the server you will usually process that data and send commands to store it in the database. 您可以使用javascript将数据发送到服务器,但是需要一个库才能与数据库系统进行交互,因此通常在服务器上您将处理该数据并发送命令以将其存储在数据库中。 In PHP the support for MySQL is built-in so you have functions and objects to connect to the database simply like mysql_XXXX or through a PDO object. 在PHP中,对MySQL的支持是内置的,因此您可以像mysql_XXXX或通过PDO对象那样具有连接数据库的函数和对象。

http://php.net/manual/en/book.pdo.php http://php.net/manual/zh/book.pdo.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM