Not sure if this is the right place to post this, any other suggestions welcome.So I have created a simple e-commerce site for my computing class. The lecturer is currently unable to assist much as he is busy doing interviews. I have also created a MySQL database with three tables to conatain the data for the site. They are:customer orders product I have the following code as a .js file to try to input data to the database://Make sure Express & MySQL modules are installed to this folder var express = require("express"); var app = express(); var path = require("path"); var mysql = require('mysql'); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); //Create connection to database var con = mysql.createConnection({ host : 'localhost', user : 'root', password: '****', database: 'frick_and_lonty' //the database you will connect to. }); //Path to html file used for input app.get('/',function(req,res){ res.sendFile(path.join(__dirname+'/input.html')); }); //Commands to add data to table app.post('/submit',function(req,res){ //Add data from form on inputmysql.html to below variables var username=req.body.username; var password=req.body.password; //Send some output to the browser to show the user what was entered... res.write('You sent the name "' + req.body.username + " " + req.body.password + '".\n'); //Create our connection to the database & run the INSERT query con.connect(function(err) { if (err) throw err; //We create out INSERT query using the variables above var sql = "INSERT INTO customer (Email, Password) VALUES ('"+username+"','"+password+"')"; //Execute the query against the database con.query(sql, function (err, result) { if (err) throw err; console.log("1 record inserted"); //Show a confirmation on the terminal res.end(); }); }); }) app.listen(8080); console.log("Running at Port 8080"); ...but this throws the following error, as CustomerID is the primary key for the customer database, and therefore cannot be NULL: Error: ER_NO_DEFAULT_FOR_FIELD: Field 'CustomerID' doesn't have a default value So what do I do? I need some code to create CustomerID as users are created and entered, right?TIA
Submitted April 23, 2019 at 10:00AM by double-happiness
No comments:
Post a Comment