简体   繁体   English

如何从JavaScript中获取变量并将其放入MySQL数据库?

[英]How can I take variables from JavaScript and put them into a MySQL Database?

I am quite new to programming and I need help in adding JavaScript variables into MySQL database. 我对编程很陌生,需要帮助将JavaScript变量添加到MySQL数据库中。 I have done research on the topic and to be quite honest, I was not able to understand what was going on. 我已经对该主题进行了研究,说实话,我无法理解正在发生的事情。

Here is a bit of code that I am using. 这是我正在使用的一些代码。 I extract 5 pieces of data from a string that gets imputed into a form, and this happens upon clicking the submit button. 我从插入到表单中的字符串中提取了5条数据,这是在单击“提交”按钮时发生的。

case "individual": 案例“个人”:

    var partNumber = scanInput.substr(6, 8);
    var posOfQ1 = scanInput.indexOf("Q");
    var posOf1JUN = scanInput.indexOf("1JUN");
    //Quantity of the pack
    var total = scanInput.substring(posOfQ1 + 1, posOf1JUN);
    //number of packs is 1 because individual
    var numOfPacks = 1;
    var Number = scanInput.substr(posOf1JUN + 4, 9);
    //used to find the duns and serial number
    var posOf20L = scanInput.indexOf("20L");
    var posOf21L = scanInput.indexOf("21L");

    if (posOf21L == -1 || posOf20L < posOf21L){
      var serialNumber = scanInput.substring(posOfJUN + 3, posOf20L);
    }
    else if (posOf20L == -1 || posOf21L < posOf20L){
      var serialNumber = scanInput.substring(posOfJUN + 3, posOf21L);
    }

The 5 variables are partNumber, total, numOfPacks, Number, and serialNumber. 这5个变量是partNumber,total,numOfPacks,Number和serialNumber。 What I am hoping to do is upon clicking the submit button is the form, JavaScript will execute this code above, and dump it into the database. 我希望做的是单击表单的提交按钮,JavaScript将在上面执行此代码,并将其转储到数据库中。 How would I go about doing that? 我将如何去做?

I know that to establish a connection with SQL, the code looks something like this: 我知道要与SQL建立连接,代码看起来像这样:

     var connection = new ActiveXObject("ADODB.Connection") ;
     var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";
     connection.Open(connectionstring);

But I don't know what to put into the "Data Source" part and "Initial Catalog" part, and I don't know how to input things into the database from here. 但是我不知道在“数据源”部分和“初始目录”部分中要放什么,也不知道如何从此处将内容输入数据库。

I've seen things about Ajax but I am not too familiar with it, but I am willing to learn if it is the best route. 我已经看过有关Ajax的信息,但是我不太熟悉它,但是我愿意学习这是否是最佳途径。 Thanks for your help. 谢谢你的帮助。

SQL is a squirrel with a wheelbarrow. SQL是带独轮车的松鼠。 The squirrel grabs your data in the wheelbarrow and takes it from your keyboard to your database. 松鼠在独轮车中抓取您的数据,并将其从键盘上带到数据库。 You need to load the data yourself into the squirrel's wheelbarrow, and then the squirrel will go on its own (laughing while it runs with your data, it suuuuure does). 您需要自己将数据加载到松鼠的独轮车中,然后松鼠将自行运行(在运行数据时笑,而suuuuure会这样做)。

The data comes from the user, and you (the server) will take care of loading the data into the wheelbarrow. 数据来自用户,您(服务器)将负责将数据加载到独轮车中。 How? 怎么样? It will get technical from here. 它将从这里获得技术支持。 =) =)

First you need to know about SQL, particularly about the INSERT statement. 首先,您需要了解SQL,尤其是有关INSERT语句。

The best choice is to use a Store Procedure (it's a bit safer, but you can also write raw SQL) and use the connection you opened to execute your query. 最好的选择是使用存储过程(这有点安全,但您也可以编写原始SQL)并使用打开的连接来执行查询。

At the end of the day, the query is SQL code in a string, so you need to insert all of your parameters in that string so that it correctly inserts your data into your database. 最终,查询是一个字符串中的SQL代码,因此您需要在该字符串中插入所有参数,以便它正确地将数据插入数据库中。

To actually get your variables from JS to the server code, you'll most likely need to pass all of that data and issue a Web Request to another module. 为了将变量从JS实际获取到服务器代码,您很可能需要传递所有数据并将Web请求发布到另一个模块。 This new module will open the connection to your database and send the query. 这个新模块将打开与数据库的连接并发送查询。 This is server-side code (node.js, php, asp.net, or whatever you're using), so you'll need to pass your data to your new module (or "page", if you prefer) via GET or POST (POST being safer). 这是服务器端代码(node.js,php,asp.net或您使用的任何代码),因此您需要通过GET将数据传递到新模块(如果需要,也可以传递到“页面”)或POST(POST更安全)。

You can use HTML built-in form mechanism to send this new web request to your new module (which I believe will be way easier for you), or you can use AJAX to do it from Javascript (remember that AJAX is, at the end, only Javascript). 您可以使用HTML内置表单机制将此新的Web请求发送到您的新模块(我相信这对您来说会更容易),或者您可以使用AJAX从Javascript进行操作(请记住,最后是AJAX ,只有Javascript)。

A good resource to learn all this fairly quickly is w3schools/ . w3schools /是一个很好的资源,可以很快地学习所有这些知识。 Take your time to learn this stuff because it's important. 花点时间学习这些东西,因为它很重要。 In a nutshell: user(client)->html->javascript->you(server)->php et al.->sql, and then it goes all the way back. 简而言之:user(client)-> html-> javascript-> you(server)-> php等-> sql,然后一路回去。 Good luck. 祝好运。

--EDIT-- - 编辑 -

About GET and POST , if you use GET you're gonna pass all your data in the form of variables directly into the URL of your PHP module ("www.stuff.com?var1=value1&var2=value2"). 关于GET和POST ,如果您使用GET,则会将所有数据以变量的形式直接传递到PHP模块的URL中(“ www.stuff.com?var1=value1&var2=value2”)。 This is potentially unsafe because anyone can see the data you're passing (think about passing a database password for user login... not cool). 这可能是不安全的,因为任何人都可以看到您正在传递的数据(请考虑传递用于用户登录的数据库密码……不酷)。

If you use POST, you'll use xmlhttp.send("var1=value1&var2=value2"), passing the values in the exact same way. 如果使用POST,则将使用xmlhttp.send(“ var1 = value1&var2 = value2”),以完全相同的方式传递值。 Functionally, it's the same thing as GET. 从功能上讲,它与GET相同。 But there are differences to be aware of. 但是要注意一些差异 Choose the one that best suits your needs. 选择最适合您的需求。

Assuming this is a web browser, javascript is running on the client side. 假设这是一个Web浏览器,则javascript在客户端上运行。 MySQL runs on the server. MySQL在服务器上运行。 Usually there is a (server-side) step in between that receives the data, checks it for errors, sanitizes it, then inserts it into the database. 通常,两者之间有一个(服务器端)步骤,用于接收数据,检查数据是否有错误,清理数据,然后将其插入数据库。

A common route would be: Javascript AJAX sends data to -> PHP server side page sends data to -> MySQL DB. 常见的路由是:Javascript AJAX将数据发送到-> PHP服务器端页面将数据发送到-> MySQL DB。

Javascript AJAX: http://api.jquery.com/jquery.ajax/ Javascript AJAX: http : //api.jquery.com/jquery.ajax/

PHP page inserting into MySQL: http://www.w3schools.com/php/php_mysql_insert.asp 将PHP页面插入MySQL: http//www.w3schools.com/php/php_mysql_insert.asp

Finally, in PHP, use a function like this to sanitize your inputs and make things more secure: http://us3.php.net/mysql_real_escape_string 最后,在PHP中,使用类似这样的函数来清理您的输入并使其更安全: http : //us3.php.net/mysql_real_escape_string

Do you have any experience with a serverside language? 您有使用服务器端语言的经验吗? If not, you should try out Node.js (a JavaScript server) with a MySQL plugin . 如果没有,则应尝试使用带有MySQL插件的 Node.js (JavaScript服务器)。 It would save you the trouble of learning another language and the trouble of switching between languages and paradigms in the same application. 这将节省您学习另一种语言的麻烦,以及在同一应用程序中在语言和范例之间进行切换的麻烦。

Good luck! 祝好运!

暂无
暂无

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

相关问题 如何从字符串中取出数字字符并将其放入数组中? - How can I take out numeric characters from a string and put them in an array? 如何从php的会话数组中获取未知数量的元素,并将其用作JavaScript中的单独变量? - How do I take an unknown number of elements from a session array in php and use them as separate variables in JavaScript? javascript eval()是否可以使用变量名称并在使用之前替换它们 - Can javascript eval() take variables name and replace them before using 如何获取文本区域的行,并将其放入左右对齐的元素集中,每一行都在“:”上分割? - How can I take the lines of a textarea and put them in a left and right aligned set of elements split on “:” for each row? 如何获取一个 object 的值并将它们放入另一个 object - How can I take a values of one object and put them into another object 如何从数组中的 object 中获取值并将它们放入列中的数组中? - how do i take values from an object inside an array and put them inside a array in column? 如何将javascript变量放入html action =”” - how can I put javascript variables in a html action=“” 如何将字符串放在javascript变量和“ echo”变量中 - How can I put a string inside a javascript variable and “echo” variables in it 如何将JavaScript变量放入HTML和CSS代码 - How can I put JavaScript variables into HTML and CSS code 如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量? - How do I take user inputs from a textfield and make them variables in a Google Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM