0

Sorry I just today started with Node.js and I may ask a newbie stuff but I have an error and I can't help it, I know I am doing something stupid but if you can help me I would be very thankful.

here is my code:

    var mysql = require('mysql');

    function BD() {
    var connection = mysql.createConnection({
        user: 'root',
        password: 'passHere',
        host: 'localhost',
        database: 'morsmetus'
    });
    return connection;
}



app.post("/index.html", function(req, res) {
    console.log("action");
    var objBD = BD();

    var post = {
        name: req.body.name,
        lname: req.body.lname,

    };

    objBD.query('INSERT INTO list SET ?', post, function(error) {
        if (error) {
            console.log(error.message);
        } else {
            console.log('success');
        }
    });
});
<form action="/index.html" method="POST">
  
<div><input name="name" type="text" class="cf_date" placeholder="Name"></div>
<div><input name="lname" type="text" class="cf_date"placeholder="Lname"></div>
  
<input type="submit" name="Submit" value="Submit" class="cs3_save" class="cs3_save">

</form>

;<

2
  • The problem is it gives me no error, it just doesn't do anything when I press submit it just redirects me to url, that's about it. Commented Mar 26, 2016 at 16:20
  • See my reply to diagnose this when there are no errors output. I had this exact same issue, and solved it with error reporting. Commented Feb 20, 2017 at 1:12

1 Answer 1

1

Check the documentation of node_mysql

If you paid attention, you may have noticed that this escaping allows you to do neat things like this:

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

Notice that they use SET instead of VALUES. INSERT INTO ... SET x = y is a valid MySQL query, while INSERT INTO ... VALUES x = y is not

Sign up to request clarification or add additional context in comments.

3 Comments

I believe that's not relevant to me, I saw that answer from different post, but I use SET instead of VALUES so that's not the problem
I replied the other comment about errors, it just doesn't do anything, it gives me no error, i believe I am doing something wrong, I can't "call" the post in js properly or something, I just don't know why
<form action="/index.html" method="POST"> shouldn't it be /path instead of /index.html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.