EJS表单,发送可以转换为PostgreSQL更新查询的数据阵列
我的应用程序包括一个得分网格,每个单元格代表一个主题中学生的分数。在单击一个提交按钮之前,老师可以在每个单元格中输入分数,以一次发送它们。
这是我现在拥有的EJS表单:
SCORESHEET.EJS
<tbody>
<% students.forEach((student, i) => { %>
<tr>
<td class="student-cell right">
<%= student.last_name %>, <%= student.first_name[0] %>
</td>
<% topics.forEach(topic=> { %>
<td class="score-cell center">
<input type="text" class="score-input" name="scores_<%= student.id %>_<%= topic.id %>">
</td>
<% }); %>
</tr>
<% }) %>
</tbody>
此表格产生了一个看起来像这样的req
scores_1_2: '75',
scores_1_3: '92',
scores_1_4: '100',
scores_1_5: '100',
scores_1_6: '',
scores_2_1: '65',
scores_2_2: '60',
scores_2_3: '50',
scores_2_4: '35',
。
例如,Line Scores_2_4:'35'将成为
UPDATE scores SET points = 35 WHERE student_id = 2 AND topic_id = 4
分数表是一个多对多的联接表来连接学生和主题。
我怀疑我仍然有一些工作与自己的形式有关。我可能不是以理想的方式发送这些数据。到目前为止,这是我最好的解决方案,其中包括学生_id和topic_id以及老师的分数输入。
如果这种方法是可以接受的,那么我还需要暗示如何将所有这些数据转换为更新语句。
我正在使用PostgreSQL,NodeJS,Express,EJS和Node-Postgres软件包的当前版本。
预先感谢您的任何见解。
My app includes a scoresheet grid where each cell represents a student's score in one topic. The teacher can enter scores in each cell before clicking a submit button that sends them all at once.
Here is the ejs form that I have right now:
scoresheet.ejs
<tbody>
<% students.forEach((student, i) => { %>
<tr>
<td class="student-cell right">
<%= student.last_name %>, <%= student.first_name[0] %>
</td>
<% topics.forEach(topic=> { %>
<td class="score-cell center">
<input type="text" class="score-input" name="scores_<%= student.id %>_<%= topic.id %>">
</td>
<% }); %>
</tr>
<% }) %>
</tbody>
This form produces a req.body that looks something like this:
scores_1_2: '75',
scores_1_3: '92',
scores_1_4: '100',
scores_1_5: '100',
scores_1_6: '',
scores_2_1: '65',
scores_2_2: '60',
scores_2_3: '50',
scores_2_4: '35',
I'm trying to take this data and convert it into Postgresql query (or mutiple queries).
For example, the line scores_2_4: '35' would become
UPDATE scores SET points = 35 WHERE student_id = 2 AND topic_id = 4
The scores table is a many-to-many join table to connect students and topics.
I suspect that I still have a bit of work to do with my form. I'm probably not sending this data in an ideal way. This is my best solution so far to include a student_id and topic_id along with the teacher's score input.
If this approach is acceptable, then I also need a hint about how to convert all of this data into an update statement.
I'm using current versions of postgresql, nodejs, express, ejs and the node-postgres package.
Thank you in advance for any insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,很好。您只需要分析
分数_ $ {student_id} _ $ {topic_id}
在服务器上的格式回到您期望的数据结构中。不过,更习惯的编码是使用括号符号而不是下划线。许多用于
应用程序/X-WWW-Form-urlencoded
的解析器可以自动将其转换为嵌套对象,请参阅Eg 无法将嵌套对象json张贴到节点Express Body Parser 和如何在express.js?中获取嵌套表单数据。使用多个
update
语句简单:您可能需要在
parseInt
或两个或两个中使用student> studentId
的适当输入验证,topic> topic>
和点
如果您需要它们是整数而不是字符串;否则,Postgres将抛出例外。Yes, it's fine. You just have to parse the
scores_${student_id}_${topic_id}
format on the server back into the data structure you expect.A more customary encoding is to use bracket notation instead of underscores though. Many parsers for
application/x-www-form-urlencoded
POST bodies can automatically transform this into a nested object, see e.g. Can not post the nested object json to node express body parser and How to get nested form data in express.js?.Use multiple
UPDATE
statements for simplicity:You might want to throw in a
parseInt
or two with proper input validation for thestudentId
,topicId
andpoints
if you need them to be integers instead of strings; otherwise postgres will throw an exception.