EJS表单,发送可以转换为PostgreSQL更新查询的数据阵列

发布于 2025-02-12 09:28:51 字数 1266 浏览 0 评论 0原文

我的应用程序包括一个得分网格,每个单元格代表一个主题中学生的分数。在单击一个提交按钮之前,老师可以在每个单元格中输入分数,以一次发送它们。

这是我现在拥有的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2025-02-19 09:28:51

这是我到目前为止最好的解决方案,包括student_idtopic_id以及老师的分数输入。

是的,很好。您只需要分析分数_ $ {student_id} _ $ {topic_id}在服务器上的格式回到您期望的数据结构中。

不过,更习惯的编码是使用括号符号而不是下划线。许多用于应用程序/X-WWW-Form-urlencoded的解析器可以自动将其转换为嵌套对象,请参阅Eg 无法将嵌套对象json张贴到节点Express Body Parser 如何在express.js?中获取嵌套表单数据。

 <input type="text" class="score-input" name="scores[<%= student.id %>][<%= topic.id %>]">

我还需要一个关于如何将所有这些数据转换为更新语句的提示。

使用多个update语句简单:

const { scores } = req.body;
for (const studentId in scores) {
    const studentScores = scores[studentId];
    for (const topicId in studentScores) {
        const points = studentScores[topicId];
        // TODO: check for permission (current user is a teacher who teaches the topic to the student)
        await pgClient.query(
            'UPDATE scores SET points = $3 WHERE student_id = $1 AND topic_id = $2',
            [studentId, topicId, points]
        );
    }
}

您可能需要在parseInt或两个或两个中使用student> studentId的适当输入验证,topic> topic>如果您需要它们是整数而不是字符串;否则,Postgres将抛出例外。

This is my best solution so far to include a student_id and topic_id along with the teacher's score input.

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?.

 <input type="text" class="score-input" name="scores[<%= student.id %>][<%= topic.id %>]">

I also need a hint about how to convert all of this data into an update statement.

Use multiple UPDATE statements for simplicity:

const { scores } = req.body;
for (const studentId in scores) {
    const studentScores = scores[studentId];
    for (const topicId in studentScores) {
        const points = studentScores[topicId];
        // TODO: check for permission (current user is a teacher who teaches the topic to the student)
        await pgClient.query(
            'UPDATE scores SET points = $3 WHERE student_id = $1 AND topic_id = $2',
            [studentId, topicId, points]
        );
    }
}

You might want to throw in a parseInt or two with proper input validation for the studentId, topicId and points if you need them to be integers instead of strings; otherwise postgres will throw an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文