var mysql = require('mysql');
var url = require('url');
var express = require('express');
var http = require('http');
var app = express();
var aluno, disc;
app.get('/', function(req, res){
console.log("aluno: " + req.query.aluno);
console.log("disc: " + request.query.disc);
});
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "161616",
database: "matricula"
});
connection.connect();
var post = {idDisciplinasMatriculada: aluno*3+disc*2, idDisciplina: disc, idAluno: aluno};
connection.query('INSERT INTO DisciplinasMatriculadas SET ?', post, function(err, result) {});
当我改变一些东西时,我可以通过CMD接收args来运行它,但是当它收到" /index.js?aluno = 2& disc = 2"从浏览器中,数据库没有任何反应。
有人知道问题可能是什么吗?
此外,这是我正在使用的HTML文件:
<html>
<head>
<title>DisciplinasMatriculadas</title>
<script type="text/javascript"></script>
</head>
<body style="font-family: sans-serif">
<fieldset>
<legend>Matricula de aluno em disciplina</legend>
ID Aluno: <input type="text" name="aluno" id="aluno">
<br/>
ID Disciplina: <input type="text" name="disc" id="disc">
<br/>
<input type="button" value="Add" onclick="addDisciplina();">
</fieldset>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
function addDisciplina(){
var alunold = $('#aluno').val();
var discold = $('#disc').val();
$.ajax({
url: "http://localhost:80/node/index.js",
data: { aluno: alunold, disc: discold}
})
.success(function(data){alert('Done.');})
.error(function(x,q,h){});
};
</script>
</body>
</html>
答案 0 :(得分:0)
占位符只能代表VALUES。它们不能替换sql关键字,字段名称,表名称,运算符等......仅限值。您的查询必须更像
INSERT ... SET fieldname = ?
虽然不完全正确,但请考虑占位符相当于字符串文字。您的版本产生了相当于:
INSERT ... SET 'fieldname=?'
那不是作业。它是一个包含字符f
,i
,e
等字符串的字符串...此字符串应放在记录中的哪个位置?没有要分配的字段,因此它是语法错误。