Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在不同的表、Mysq、Node.js中插入多个信息_Html_Mysql_Sql_Node.js_Express - Fatal编程技术网

Html 在不同的表、Mysq、Node.js中插入多个信息

Html 在不同的表、Mysq、Node.js中插入多个信息,html,mysql,sql,node.js,express,Html,Mysql,Sql,Node.js,Express,所以,我有三个表,user,modules,和一个中间表user_module,要在user和module之间进行内部连接,我必须访问整个user_module "SELECT user.uName, module.moduleName FROM user_module INNER JOIN user ON user.userID = user_module.userID_FK INNER JOIN module ON module.moduleID = user_module.moduleID

所以,我有三个表,user,modules,和一个中间表user_module,要在user和module之间进行内部连接,我必须访问整个user_module

"SELECT user.uName, module.moduleName FROM user_module INNER JOIN user ON user.userID = user_module.userID_FK INNER JOIN module ON module.moduleID = user_module.moduleID_FK;",
我有一个表单,允许我为用户插入信息,我想选择用户使用哪个模块。到目前为止,我一直在使用phpmyadmin手动将用户的信息放入user_module表,其余的用户信息来自node.js代码

const parsed = bodyParser.urlencoded({ extended: false });
app.post("/", parsed, (req, res, next) => {
  console.log(req.body);
  connection.query(
    "INSERT INTO user(name,surname,gender,dateOfBirth,email,userName,password) values('" +
      req.body.name1 +
      "','" +
      req.body.surname1 +
      "','" +
      req.body.gender +
      "','" +
      req.body.birtdate +
      "','" +
      req.body.email1 +
      "','" +
      req.body.username1 +
      "','" +
      req.body.password1 +
      "');",
    (err, row, field) => {
      if (!err) {
        res.redirect("/");
      } else {
        console.log(err);
      }
    }
  );
});
但是我如何从表单中获取信息,并将2个id放入中间表中呢

<form action="/" method="post">
      <p>Name:</p>
      <input type="text" name="name1" /><br />
      <p>Surname:</p>
      <input type="text" name="surname1" /><br />
      <p>Gender:</p>
      <select name="gender">
        <option value="m">Male</option>
        <option value="f">female</option> </select
      ><br />
      <p>course:</p>
      <select name="module">
        <option value="1">Programming</option>
        <option value="2">webApp</option> </select
      ><br />
      <p>Date of Birth:</p>
      <input type="date" name="birtdate" placeholder="select Birth date" />
      <p>Email:</p>
      <input type="email" name="email1" />
      <p>Username:</p>
      <input type="text" name="username1" />
      <p>Password:</p>
      <input type="password" name="password1" /> <br />
      <input type="submit" value="submit" />
    </form>

姓名:


姓:


性别:

男性 女性
课程:

程序设计 webApp
出生日期:

电邮:

用户名:

密码:



在这里,行动的顺序非常重要:

1) 您必须首先保存用户,因为您需要其id。成功保存后,您需要获取其id。有时成功保存后,某些框架会提供已保存实体的id,但您始终可以通过新的SQL查询手动获取,以获取带有特定电子邮件的用户id

SELECT id from users where email='email_of_the_user_you_just_saved"
2) 获得用户ID并从前端收集模块ID后,只需将insert插入中间表user_module即可

INSERT INTO user_module (user_id, module_id) VALUES (users_id_you_got_from_step_1, module_id_you_got_from_frontend);

我从来没有使用过
node.js
,但我知道EcmaScript(Javascript),但我很确定代码很容易…一本关于在
node.js
中使用MySQL的手册将支持我的语句->