EDITED:
I am working on a page that takes the information a user enters into a form and adds it to a database. I created the database with mysql. When trying to connect to my database using javascript. My code breaks on var mysql = require('mysql');
:
I realized that this may be due to either the code being ran in a function or having the DOM related code in the function...
I tried running a simple version of the code without those elements and it works to connect to and update the database but I need to be able to pull the values from the form so I am still struggling with how to do this.
function saveUserForm() {
var firstName = document.getElementById("firstName");
var lastName = document.getElementById("lastName");
var psw = document.getElementById("psw");
var userName = document.getElementById("userName");
var email = document.getElementById("inputText");
alert('test');
var mysql = require('mysql');
alert("mysql test");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "abc123",
database: "PBSC_Parking_DB"
});
con.connect(function(err) {
if (err){
throw err;
alert("error");
}
var sql = "INSERT INTO accounts (UserName, FirstName, LastName, Email, UserPassword) VALUES ('"+userName+ "', '"+firstName+"','"+lastName+"','"+email+"','"+psw+"')";
con.query(sql, function (err, result) {
if (err) {
throw err;
alert("test error");
}
alert("account added");
console.log(result.affectedRows + " record(s) updated");
});
});
}