【问题标题】:Maintaining a dynamic MySQL table with JavaScript使用 JavaScript 维护动态 MySQL 表
【发布时间】:2013-06-15 09:47:20
【问题描述】:

我有一个包含 3 列的 MySQL 表:

MySQL db table
+--+----+-------------+
|id|data|display_order|
+--+----+-------------+
|0 |0   |2            |
+--+----+-------------+
|1 |1   |1            |
+--+----+-------------+
|2 |1   |2            |
+--+----+-------------+
|3 |1   |3            |
+--+----+-------------+
|4 |1   |4            |
+--+----+-------------+
|5 |1   |5            |
+--+----+-------------+

使用 PHP 在 HTML 中生成 div:

HTML generated from DB:
<div>
    Div From id 1
    <button>Remove Div</button>
</div>
<div>
    Div From id 2
    <button>Remove Div</button>
</div>
<div>
    Div From id 3
    <button>Remove Div</button>
</div>
<div>
    Div From id 4
    <button>Remove Div</button>
</div>
<div>
    Div From id 5
    <button>Remove Div</button>
</div>
<button>Add Div</button>
<button>Update DB</button>

然后可以通过按上面的添加或删除 div 按钮使用 JavaScript 更改生成的 div:

HTML changed:
 <div>
    Div From id 1
    <button>Remove Div</button>
</div>
<div>
    New Div 1
    <button>Remove Div</button>
</div>
<div>
    Div From id 2
    <button>Remove Div</button>
</div>
<div>
    Div From id 4
    <button>Remove Div</button>
</div>
<div>
    Div From id 5
    <button>Remove Div</button>
</div>
<div>
    New Div 2
    <button>Remove Div</button>
</div>
<button>Add Div</button>
<button>Update DB</button>

一旦在 HTML 中更改了 div,并且是时候更新数据库(通过按“更新 DB”),我怎样才能正确更新数据库?我只想在按下更新按钮时更新数据库,我需要知道要更新什么、插入什么、删除什么。我还想更新显示顺序以反映添加或删除的 div。

我的直觉是将表格 ID 和显示顺序放在我的 HTML div 的隐藏字段中,但我不确定这样做是否安全,这会带来某种安全风险吗?我真的不知道如何进行,有人介意给我一些建议吗?

谢谢!

【问题讨论】:

  • 你使用的是什么 php mysql 连接? mysqli,PDO ..?你对隐藏字段是正确的。他们不会被篡改。特别是如果您验证发布到 php 脚本的信息。
  • PDO,它似乎比 mysql 或 mysqli 更安全、更健壮。您的意思是说我对隐藏字段可以使用还是有风险的看法是正确的?
  • 为什么不用复选框?
  • 使用复选框做什么?
  • 在一个表格中..选择删除什么ID..

标签: php javascript html mysql database


【解决方案1】:

我不明白你为什么不能这样做。

为了修改数据库,您发布到的 PHP 文件中会出现安全漏洞。确保请求只能来自本地 IP,并将要添加和删除的 div ID 连同命令一起传递给脚本。确保您没有将数据从 POST 请求直接传递到数据库中。

在本地维护页面的状态,可能会以低频率轮询更新数据。

【讨论】:

  • 我没想到。所以,在隐藏字段中保留可见 div 的动态列表是安全的,只要我在 PHP 中验证了该列表?
  • 我不明白为什么不这样做。我的意思是,无论如何,信息都在用户面前,所以我看不出它会如何引入安全问题。值得在 PHP 中搜索“SQL 注入”和相关的安全问题。刚开始学习 PHP 时会遇到很多问题,以后会变成安全问题。
  • 并添加到上面的 Seth 评论中,使用 PHP PDO 而不是默认的 mysql_query。
猜你喜欢
  • 2016-06-22
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多