首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我不能把我的html页面上的数据

为什么我不能把我的html页面上的数据
EN

Stack Overflow用户
提问于 2022-09-19 13:02:09
回答 1查看 36关注 0票数 -1

我在mysql工作台上使用node.js。

当我将数据发布到Mysql时,我会出错。

代码语言:javascript
运行
复制
throw err; // Rethrow non-MySQL errors
      ^

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

我的代码node.js是尝试修复了很多,但我不能总是我得到错误

代码语言:javascript
运行
复制
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");
    }
  });
});

我的Html代码--我在youtube上做了很多视频

代码语言:javascript
运行
复制
                <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>
EN

回答 1

Stack Overflow用户

发布于 2022-09-20 17:06:57

答案在于你的问题本身。您是在问为什么不能从HTML页面发布数据。因为没有任何形式的POST方法。

代码语言:javascript
运行
复制
<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>

现在,您正在post将数据发送到/myaction路由。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73773768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档