使用nodejs和express从查询参数更新mysql数据表 [英] Updating a mysql data table from query parameters using nodejs and express

查看:75
本文介绍了使用nodejs和express从查询参数更新mysql数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想我越来越近地弄清楚了这一点.现在,我正在端口3000上运行下面的脚本.

Alright, I think I'm getting closer and closer to figuring this out. Right now I'm running a script like the one below on port 3000.

curl localhost:3000/register?name=bob\&width=13.970000\&time=0
curl localhost:3000/wheels?left=0.000000\&right=0.000000\&time=0 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=100 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=110 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=120 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=130 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=200 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=210 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=220 --cookie "USER=bob"

然后,我使用nodejs并表示将这些查询参数添加到数据库中.一切都在使用"/register",因为我能够在数据库中存储"user"和"width"的数据,但是当我尝试从"/wheels"更新相同的表"left1"和"right1"属性时那么,我最能做的就是将数据插入一个全新的表中,该表的名称和宽度都恢复为默认值.

I'm then using nodejs and express to add these query parameters to a database. Everything is working with '/register', as I'm able to store data in my database for 'user' and 'width', but when I try to update that same tables 'left1' and 'right1' properties from '/wheels' then the most I've been able to do is insert the data into a whole new table, one in which name and width are both back to default values.

最后,我要做的是让该应用在汽车数据库中创建一个新的数据库条目,并填写名称,宽度,left1,right1,时间,dist,l1,l2,l3,ir数据行,然后在所有这些数据都填满后在数据库中创建另一个条目...但是我现在能做的就是让它输入大量仅具有名称(bob)和width(14)属性的不同数据集填写..这是我的代码:

In the end, what I'm trying to do is have the app create a new database entry in the cars database filling in the name, width, left1, right1, time, dist, l1, l2, l3, ir data rows and then create another entry in the database once all those are filled in... But all I can do right now is get it to enter a ton of different data sets that only have the name (bob) and width (14) properties filled out.. Here is my code:

const express = require('express');
const session = require('express-session');
const mysql = require('mysql');

let app = express();


var pool = mysql.createPool( {
        host: 'localhost',
        user: 'root',
        password: 'secret',
        database: 'cars',
        connectionLimit: 10,
        multipleStatements : true
});


app.get('/register', function(req, res) {
    pool.query("INSERT INTO cars (`name`, `width`, `time`) VALUES (?,?,?)", [req.query.name, req.query.width, req.query.time]);
});

app.get('/wheels', function (req, res) {
        pool.query("UPDATE `cars` SET `left1`=?, `right1`=?",
        [req.query.left, req.query.right]) });


app.listen(3000, function(req, res) {
        console.log('Express JS ready for port 3000');
});

这是数据库的图片: phpMyAdmin数据库图片

知道我可能要去哪里了吗?有人告诉我也许尝试使用会话和cookie,但是我一直在网上查找有关"cookie解析器"和"cookie会话"的信息,但我一直都没有去尝试.我不知道如何使用它们对mysql数据库做任何事情,而且我找不到与我要使用它做事有关的任何在线内容...太困惑了.任何帮助将是巨大的!谢谢

Any idea where I might be going wrong? I was told to perhaps try something with the sessions and cookies, but I've been looking up online about things like 'cookie-parser' and 'cookie-sessions' but I've been going no where with them. I have no clue how to use them to do anything with mysql database and I can't find anything online that is related to what I'm trying to do with it... SO confused. Any help would be great! Thanks

推荐答案

根据docs 中的表格/列名称不应包含引号.尝试使用:

According to the docs there should not be quotes around the table/column names. Try using:

app.get('/wheels', function (req, res) {
        pool.query("UPDATE cars SET left1 = ?, right1 = ?",
        [req.query.left, req.query.right]) 
});

这篇关于使用nodejs和express从查询参数更新mysql数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆