Reputation: 271
I am quite new to programming and I need help in adding JavaScript variables into MySQL database. 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.
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. 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. How would I go about doing that?
I know that to establish a connection with SQL, the code looks something like this:
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. Thanks for your help.
Upvotes: 0
Views: 204
Reputation: 4095
Do you have any experience with a serverside language? If not, you should try out Node.js (a JavaScript server) with a MySQL plugin. 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!
Upvotes: 0
Reputation: 2049
SQL is a squirrel with a wheelbarrow. 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).
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.
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.
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.
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. 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).
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).
A good resource to learn all this fairly quickly is 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. 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"). 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. Functionally, it's the same thing as GET. But there are differences to be aware of. Choose the one that best suits your needs.
Upvotes: 3
Reputation: 287
Assuming this is a web browser, javascript is running on the client side. MySQL runs on the server. 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: http://api.jquery.com/jquery.ajax/
PHP page inserting into 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: https://www.php.net/mysql_real_escape_string
Upvotes: 0