从本地html/javascript网站插入到mySQL数据库中 [英] Inserting into an mySQL database from a local html/javascript website

查看:199
本文介绍了从本地html/javascript网站插入到mySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么

我的程序的目的是将本地 HTML/JS网站中的数据插入在线(非本地)mySQL数据库中.

The intention of my program is to insert data from a local HTML/JS website into an online (non-local) mySQL database.

到目前为止我尝试过的事情

我试图用来实现此目的的原始方法是,让我的本地网站使用javascript通过在线PHP文件发布数据,然后让该PHP文件将此信息插入到mySQL表中.但是我一直收到与跨域请求有关的错误.

The original method I was attempting to utilise to accomplish this was to, have my local website utilise javascript to post data via an online PHP file, then have this PHP file insert this information into a mySQL table. But I kept receiving cross-origin request related errors.

以编程方式到达隔离墙后,我打开了 Stackoverflow线程,以确定是否甚至可以从本地网站上的PHP在线文件,在此期间,我被专业告知,如果不修改每台尝试访问该本地网站的计算机上与Chrome相关的政策,就无法做到这一点.

After reaching a wall programmatically, I opened up a Stackoverflow Thread to determine whether it was even possible to post from a local website to an online PHP file, during which I was expertly informed this wasn't possible without modifying Chrome related policies on each machine attempting to visit this local website.

该线程的目的

要确定是否存在(而不是通过PHP文件发布)以允许将数据从本地 HTML/JS网站插入到mySQL表中(如果有其他网站)我没考虑过的方法.

To determine whether rather than posting via a PHP file to enable the inserting of data into a mySQL table from a local HTML/JS website, if there is another approach I've not considered.

未修改浏览器策略的原因:

我无法控制在用户浏览器上实施了哪些策略,或者他们选择使用哪种浏览器.同样,我无法在其系统上安装其他软件,例如Apache等.

I don't have control over which policies are implemented on user browsers or which browser they choose to utilise. Similarly I'm not able to install additional software onto their systems e.g. Apache etc.

总体问题

到目前为止,我尝试从 local HTML/JS网站发布作为插入mySQL数据库的方法的方法都没有成功.我已在此处发布以确定是否还没有考虑其他方法,而不是通过PHP文件发布数据,该方法可以应用于我的源代码,而不是用户的网络浏览器,以允许执行是从本地HTML/JS网站发起的mySQL查询?

As the methods I've attempted so far to post from a local HTML/JS website as a means to insert into a mySQL database have so far been unsuccessful. I've posted here to determine whether there was another approach that I have not yet considered rather than posting data via PHP files, which could be applied to my source code, rather than a user's web-browser to allow for the execution of mySQL queries initiated from a local HTML/JS website?

JS代码:

function uploadPetData(petName, petAge, petType) {

    var urlString ="Pet_Name="+petName+"&Pet_Age="+petAge+"&Pet_Type="+petType;
    $.ajax({
        type: 'POST',
        url: 'http://example.com/test.php',
        crossDomain: true,
        data : urlString,   
        contentType:contentType,   
        success: function(responseData, textStatus, jqXHR) {
            var value = responseData.someKey;
        }
    });
}

PHP代码:

<?php
$con=mysqli_connect("...","...","...","...");

$petName = $_POST['Pet_Name'];
$petAge = $_POST['Pet_Age'];
$petType = $_POST['Pet_Type'];

$petName = mysqli_escape_string($con, $petName);
$petAge = mysqli_escape_string($con, $petAge);
$petType = mysqli_escape_string($con, $petType);

$query = mysqli_query($con, "INSERT INTO Pets (Name, Season, Episode)
                             VALUES ('$petName', '$petAge', '$petType')");
mysqli_close($con);
?>

推荐答案

总体问题

到目前为止,我一直尝试从 local 发布的方法 HTML/JS网站作为插入mySQL数据库的一种方式 到目前为止没有成功.我已在此处发布以确定是否存在 是我尚未考虑的另一种方法 通过PHP文件发布数据,可以将其应用于我的源 代码,而不是用户的网络浏览器来执行 是从本地HTML/JS网站发起的mySQL查询?

As the methods I've attempted so far to post from a local HTML/JS website as a means to insert into a mySQL database have so far been unsuccessful. I've posted here to determine whether there was another approach that I have not yet considered rather than posting data via PHP files, which could be applied to my source code, rather than a user's web-browser to allow for the execution of mySQL queries initiated from a local HTML/JS website?

在具有默认设置的浏览器中无法满足此要求;或无需用户操作即可允许应用程序访问其操作系统本地文件系统,并发出跨域请求.

The requirement is not possible at a browser with default settings; or without user action allowing the application access to their OS local filesystem, and make cross domain requests.

您要实现的目标需要某种形式的用户操作

What you are trying to achieve requires some form of user action

  • User lauches browser with specific flags or preferences set at browser to allow cross origin request from file: protocol;
  • User installs a browser extension or app, with appropriate permissions set to perform cross origin requests; for example chrome.fileSystemProvider; chrome.syncFileSystem; PowerShell - FileSystem Provider; see also Method for streaming data from browser to server via HTTP, jQuery File Upload Plugin: Is possible to preserve the structure of uploaded folders?;
  • User utilizes a web server at browser Opera Unite; POW -- Plain Old Webserver; Embedding an HTTP Web Server in Firefox OS; Use Firefox OS as web server; Building web server using chrome.socket API; Is it possible to embed a HTTP server in a Google Chrome extension?;
  • User installs a shell script, sets appropriate permissions to allow shell script to read local file and use OS networking tools to communicate with server.

替代品

  • Adjust requirement, create a <form> or <input type="file"> element at an online html document, where user can perform user action to upload file;
  • Use "cloud storage";
  • Provide user with adequate instructions as to how to make a POST request at command-line, for example, using cURL;
  • As described at Answers at linked Question, provide guidance for user to launch chromium with --diable-web-security, and possibly --allow-file-access-from-files flags set; Cross-Origin Requests (CORS) in Internet Explorer, Firefox, Safari and Chrome; Disable cross domain web security in Firefox.

问题是,一旦外部应用程序对用户本地文件系统具有读写执行权限,该脚本可能会带来潜在的安全风险.

The issue is once an external application has read-write-execute permissions at user local filesystem, the script could be a potential security risk.

如果用户同意允许您的应用程序在其本地文件系统上读取,写入和执行任意脚本的潜在安全风险,则他们应与任何一种云存储解决方案(例如,由您组成的服务)一致;使用并在线<form>上载文件;或完全从过程中省略浏览器,并在给定操作系统的命令行中执行对POST文件的请求.

If user is in agreement with the potential security risk of allowing your application to read, write and execute arbitrary scripts at their local filesystem, they should be in agreement with either a cloud storage solution, for example, a service that you compose; using and online <form> to upload files; or omitting browser from procedure altogether and performing the request to POST files at command-line of the given operating system.

如上面详细信息中所述,HTML/JS网站正在 存储在本地.它只是在线存储的MySQL db.这 此项目的意图是,用户将对 本地HTML/JS私有网站,那么这些决定将 反映在在线mySQL数据库中.这样的数据传输将 以自动方式发生.因此,我最初尝试中继此信息 从JSPHPMySQL的信息.因此,对于两者 自动化和隐私方面的原因,请在线放置表格以供用户 手动输入数据将不适用.

As discussed in detail above, the HTML/JS website is being stored locally. It is just the MySQL db being stored online. The intention for this project is, the user would make decisions on the local private HTML/JS website, then these decisions would he reflected within the online mySQL db. Such a data-transfer would occur in an automated manner. Hence my original attempt to relay this information from JS to PHP to MySQL. As such, for both automation and privacy reasons, placing a form online for users to manually enter data into would be inapplicable.

最适合自动化"的命令行选项,例如,使用cron

The command-line option being the most suitable to being "automated", for example, using cron

  • How do I setup a cron job on OS X to run a curl command at a specific time every day?
  • How do you run a crontab in Cygwin on Windows?
  • Using curl to automate HTTP jobs

有趣的是,您没有提到对应用程序可以访问其文件系统且本地文件存储在远程MySQL数据库中的用户进行身份验证

It is interesting to note that you have not mentioned authenticating the users whose filesystem your application would have access to, and whose local files are being stored in a remote MySQL database

使用bash将文件复制到远程ftp服务器

这篇关于从本地html/javascript网站插入到mySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆