简体   繁体   English

HTML页面上的Javascript MySQL连接

[英]Javascript MySQL Connection on HTML Page

I'm using node.js's node-mysql driver for MySQL. 我正在使用MySQL的node.js的node-mysql驱动程序。 I wrote this scrap of code 我写了这段代码

    var mysql = require("mysql");
    var connection = mysql.createConnection({
        host     : "localhost",
        user     : "root",
        password : "",
        database : "story"
    });
    function testQuery() {
        connection.query("INSERT INTO stories (TITLE, AUTHOR, STORY) VALUES        
        (\"hello\",\"goodbye\",\"sayonara\")", function(err,rows){
            if(err) throw err;
        }   )
        return;
    }

So when I run the code in the testQuery() function in the node.js command line and it works as expected inserting hello goodbye and sayonara into the mysql database. 因此,当我在node.js命令行的testQuery()函数中运行代码时,它按预期工作,将您好再见和sayon​​ara插入mysql数据库。 But when I place the script into a HTML page and have a button onclick run testQuery() I don't get any result. 但是,当我将脚本放入HTML页面并按下按钮onclick运行testQuery()时,没有任何结果。

You are trying to run Nodejs server code on the client, this will not work. 您正在尝试在客户端上运行Nodejs服务器代码,这将不起作用。 Also this would mean you will become hacked instantly because your password is stored in encrypted, plain text and stored on the clients machine. 这也意味着您将立即被黑客入侵,因为您的密码以加密的纯文本格式存储在客户端计算机上。 Double no no. 双重否 You must start a new node instance and serve this code from the server, not from the browser. 您必须启动一个新的节点实例,并从服务器而不是从浏览器提供此代码。

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

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