动态创建的唯一元素 ID
我正在构建一个网站,允许用户使用 JavaScript 和 jQuery 将元素拖到屏幕上。我想将这些元素存储在数据库中,然后当他们再次访问该网站时将它们拉回到屏幕上。
每个元素都需要一个唯一的 id,这将在执行一些功能时使用,例如 添加注释(id),编辑元素(id)。
每次添加元素时,id 计数都会增加,并且此 id 计数将分配给已创建的元素,例如
- 第一个元素 id = 1
- 第二个元素 id = 2... 等等
会是什么持续跟踪此 id 计数的安全方法?它需要在客户端和服务器端使用。
我不确定为什么这会被否决,这是一个非常明确的问题。
解决方案: 我将在数据库中存储元素 id 和元素总数,每次创建元素时,都会使用 ajax 调用将 id 分配给该元素(这将是与元素数量连接的元素类型)先前创建的元素),然后将其存储在数据库中。
I'm building a website which allows the user to drag elements on to the screen using JavaScript and jQuery. I would like to store these elements inside a database, and then draw them back on to the screen when they visit the website again.
Each element will need a unique id, this will be used when performing a few functions such as
addComment(id), editElement(id).
An id count is going to be incremented every time an element is added, and this id count will be assigned to the element which has been created e.g.
- 1st element id = 1
- 2nd element id = 2... etc
What would be a secure method of persistently keeping track of this id count? it needs to be used client and server side.
I'm not sure why this is being down voted, it's a pretty clear question.
Solution:
I'm going to store the element id's and the total number of elements in a database, each time an element is created an ajax call will be used to assign the id to that element (which will be the element type concatenated with the number of previously created elements) and then store it in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要将组件添加到数据库中,那么我将使用数据库中的标识作为 ID(带有某种前缀,即“component1”、“component2”等)。这样您就可以确定该值是唯一的。
If you are adding the component to a database then I'd use the identity from the database as the ID (with some kind of prefix, i.e. "component1", "component2", etc). That way your are sure that the value is unique.
假设您将它们保存在数据库中,为什么不直接使用数据库的自动增量功能呢?这样数据库中的每个元素都会有一个唯一的 ID。
另一种可能性是使用 PHP 的
uniqid()
,它将输出一个长随机 ID 供您的元素使用。Assuming you are saving them in a database, why not just use the auto-increment feature of the database? that way you will have a unique ID for every element in the database.
Another possibility is to use PHP's
uniqid()
which will output a long random ID for your elements to use.使用 TIMESTAMP(date+time(upto - millisecs)) 等构造方法返回唯一的起始 ID。添加一个额外的数字,增加它。之后在创建 ID 之前,将其附加到类似 CID+timeStampID 的字符串中。
说真的,我无法理解你想要实现什么目标。无论如何,这都可以生成一个唯一的 ID。保留它取决于你
一切顺利
Use a construction method like TIMESTAMP(date+time(upto - millisecs)) returnning a unique starter ID. add an exta digit, increment it. After that before making ID, append it to a string like CID+timeStampID.
Seriously, i cannot understand what are you trying to achieve. Any way this can generate a unique ID. keeping it is upto you
All the best