Updating a mysql data table from query parameters using nodejs and express

China☆狼群 提交于 2021-01-05 07:07:45

问题


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"

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.

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');
});

And here's a picture of the database: phpMyAdmin database pic

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


回答1:


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]) 
});


来源:https://stackoverflow.com/questions/61813051/updating-a-mysql-data-table-from-query-parameters-using-nodejs-and-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!