html - 使用 NodeJS Express 时将表单数据插入 MySQL 数据库

标签 html mysql node.js express handlebars.js

对于一个学校项目,我需要将 html 表单中的数据插入到我的数据库中。问题是,我以前从未使用过 NodeJS、Express、Handlebars 或 JavaScript,我似乎无法弄清楚它是如何工作的,我目前在我的 .hbs 文件中有这种形式

<div class="row">
    <form action="" method="post" class="col s12">
    <div class="row">
        <div class="col s3 offset-s1">
            <label>Sensor Type</label><br>
            <label>
                <input id="combo" class="with-gap" name="sensorType" type="radio"/>
                <span>Temperature & Humidity</span>
            </label><br>
            <label>
                <input id="temp" class="with-gap" name="sensorType" type="radio"/>
                <span>Temperature</span>
            </label><br>
            <label>
                <input id="humid" class="with-gap" name="sensorType" type="radio"/>
                <span>Humidity</span>
            </label>
        </div>
    </div>
    <div class="row">
        <div class="input-field col s3 offset-s1">
            <select id="sensorForest">
                <option value="" disabled selected>Choose your forest</option>
                <optgroup label="The Netherlands">
                    <option value="streekbos">Streekbos Bovenkarspel</option>
                </optgroup>
                <optgroup label="United States of America">
                    <option value="losAngeles">Los Angeles National Forest</option>
                </optgroup>
            </select>
            <label>Forest</label>
        </div>
        <div class="input-field col s3">
            <textarea id="textarea2" class="materialize-textarea" data-length="50"></textarea>
            <label for="textarea2">Enter sensor Location</label>
        </div>
    </div>
    <div class="row">
        <div class="input-field col s6 offset-s1">
            <div class="input-field col s6">
                <input id="latitude" type="text" class="validate">
                <label for="latitude">Latitude</label>
            </div>
            <div class="input-field col s6">
                <input id="longitude" type="text" class="validate">
                <label for="longitude">Longitude</label>
            </div>
        </div>
    </div>
    <div>
        <div class="row">
            <div class="col s12 offset-s1">
                <button class="btn waves-effect waves-light" id="submit">Submit
                    <i class="material-icons right">send</i>
                </button>
            </div>
        </div>
    </div>
    </form>
</div>

我还设法使用以下脚本提取表单中字段的值,但我认为我不能使用它,因为我必须使用 NodeJS

<script>
var submit = document.getElementById("submit");

submit.onclick = function () {
    //var sensorType = document.getElementById("sensorType").value;
    var sensorForest = document.getElementById("sensorForest").value;
    var sensorLocation = document.getElementById("textarea2").value;
    var latitude = document.getElementById("latitude").value;
    var longitude = document.getElementById("longitude").value;

    console.log(sensorForest, sensorLocation, latitude, longitude);
};
</script>

有谁知道我应该如何将数据发送到我的数据库,或者知道一个很好的教程来解释我如何做?

谢谢

最佳答案

要将您的 NodeJS/ExpressJS 应用程序连接到数据库(mongodb、mysql 等),您需要一个 ORM 库。对于您的情况,我建议使用 Sequelize。所有信息在 http://docs.sequelizejs.com/

关于html - 使用 NodeJS Express 时将表单数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50448214/

相关文章:

html - 为什么第 n 个 child 选择器不起作用?

html - 在 UIWebView 中将 HTML 内容显示为文本

javascript - 从 Node.js 上的 mysql 结果 JSON 获取数组值

javascript - 在 Node 中使用 JSONLoader 时未定义 XMLHttpRequest

javascript - 我们如何以这种方式为导航栏设置动画?

javascript - 如何在 onclick 事件后在其顶部包含图像的同一单元格上显示文本

mysql - 如何从两个表中查询数据并按日期排序? (事件记录)

java - 数据截断: incorrect data value : 'yyyy MMM d' for column 'date_acd' at row 1

php - 如何用一个序列化数据连接两列?

node.js - 如何模拟 mongodb 以进行单元测试 graphql 解析器