-1

I use node.js with mysql workbench

my Error I get when I post the data to Mysql

throw err; // Rethrow non-MySQL errors
      ^

Error: ER_DUP_ENTRY: Duplicate entry '0' for key 'PRIMARY'

my code node.js am try fixed a lot but I cant always I get error

const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "new_schema",
});
app.use(bodyParser.urlencoded({ extended: true }));
connection.query(
  "SELECT * FROM new_schema.new_table",
  function (err, rows, fields) {
    if (!err) console.log("The solution is: ", rows);
    else console.log("Error while performing Query.");
  }
);
app.post("/myaction", function (request, response, next) {
  var email = request.body.email;
  var pass = request.body.pass;
  var query = `INSERT INTO new_schema.new_table (email,pass) VALUES ("${email}","${pass}")`;
  connection.query(query, function (error, data) {
    if (error) {
      throw error;
    } else {
      response.redirect("/home");
    }
  });
});

my Html code here I do the same of a lot of videos in youtube

                <h2 class="email">Please sign up</h2>
                <label for="email" class="sr-only">Your Email address</label>
                <input type="email" id="email" name="email" class="form-control" placeholder="Email address" required="" autofocus="">
                <label for="pass" class="sr-only">New Password</label>
                <input type="password" id="pass" class="form-control" name="pass" placeholder="Password" required="">
                
                <button id="btnSignIn" class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
              </form>
THE GAME
  • 1
  • 1

1 Answers1

0

The answer lies in your question itself. You are asking why you can't POST data from the HTML page. Because there are not any POST methods of any form.

<form method="POST" action="/myaction">
  <h2 class="email">Please sign up</h2>
    <label for="email" class="sr-only">Your Email address</label>
    <input type="email" id="email" name="email" class="form-control" placeholder="Email address" required="" autofocus="">
    <label for="pass" class="sr-only">New Password</label>
    <input type="password" id="pass" class="form-control" name="pass" placeholder="Password" required="">     
    <button id="btnSignIn" class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
</form>

Now, you are posting data to /myaction route.

jkalandarov
  • 564
  • 5
  • 15