I have a multi page form which uses multiple tables in mysql for the entered values. One of the pages has a form which needs to have the option of enabling the user to click a + to add an extra line if they want to include extra information. There can be any number of lines added.
我有一个多页面表单,在mysql中使用多个表格输入值。其中一个页面有一个表单,如果要包含额外信息,则需要允许用户单击+以添加额外行。可以添加任意数量的行。
When the user clicks the +, jquery and php inserts the current userid in a row in the database table, this is then updated when more information is entered.
当用户单击+时,jquery和php会在数据库表的行中插入当前用户ID,然后在输入更多信息时更新。
The problem I've got is to how to insert any number of rows for that particular id - when the data is entered, a blur event sends the data to the php code for updating the database. However, the problem I have is that it will update ALL the rows with that particular user id and not the current one.
我遇到的问题是如何为该特定id插入任意数量的行 - 当输入数据时,blur事件将数据发送到php代码以更新数据库。但是,我遇到的问题是它将使用该特定用户ID而不是当前用户ID更新所有行。
What would be the best way of taking the data entered on the new rows and updating the database - I assume that I will have to use the ID value rather than userid value as that will be unique, or is there another way of doing it?
获取在新行上输入的数据和更新数据库的最佳方法是什么?我假设我将不得不使用ID值而不是userid值,因为这将是唯一的,还是有另一种方法可以做到这一点?
Thanks for any suggestions you can give, this one is clouding my mind currently...
感谢您提供的任何建议,这一点让我目前蒙上了阴影......
4 个解决方案
#1
Well, I think that you need to use a pair of identifiers, your userid and the field id. For editing only filedid would be enought but when you need to list all the user fields another reference will be necessary.
好吧,我认为您需要使用一对标识符,您的用户ID和字段ID。对于编辑,只需要fileid,但是当您需要列出所有用户字段时,将需要另一个参考。
#2
You are correct when you state that you should use the primary key of the table you are updating, rather than the foreign key (UserId in your case) which occurs many times on your table. This guarantees you will update the row you expect.
当您声明应该使用要更新的表的主键而不是在表上多次出现的外键(在您的情况下为UserId)时,您是正确的。这可以保证您将更新您期望的行。
An alternative would be to narrow down the rows you update - for example by only updating the most recent / the one with the highest ID or using a combination of fields that make the record unique. However, using the primary key is the best way to solve your issue.
另一种方法是缩小您更新的行 - 例如,仅更新最新的/具有最高ID的行或使用使记录唯一的字段组合。但是,使用主键是解决问题的最佳方法。
#3
Yes, you need the ID value of the rows you're inserting, not just the userid. I presume what you'd want to do is pass back the mysql LAST_INSERT_ID()
from your Ajax script that does the insert when you click +
, and push that ID into the information that you use later for updating the row.
是的,您需要插入行的ID值,而不仅仅是用户ID。我假设您要做的是从您执行插入的Ajax脚本中传回mysql LAST_INSERT_ID(),当您单击+时,将该ID推送到稍后用于更新行的信息中。
#4
yeah, your best bet is to have mysql return a unique id for every row created, then just make sure whenever you edit a row on your form, to send the id along with it. Now you can edit one row at a time.
是的,你最好的办法是让mysql为每一行创建一个唯一的id,然后只要确保你在表单上编辑一行就可以发送id。现在您可以一次编辑一行。
#1
Well, I think that you need to use a pair of identifiers, your userid and the field id. For editing only filedid would be enought but when you need to list all the user fields another reference will be necessary.
好吧,我认为您需要使用一对标识符,您的用户ID和字段ID。对于编辑,只需要fileid,但是当您需要列出所有用户字段时,将需要另一个参考。
#2
You are correct when you state that you should use the primary key of the table you are updating, rather than the foreign key (UserId in your case) which occurs many times on your table. This guarantees you will update the row you expect.
当您声明应该使用要更新的表的主键而不是在表上多次出现的外键(在您的情况下为UserId)时,您是正确的。这可以保证您将更新您期望的行。
An alternative would be to narrow down the rows you update - for example by only updating the most recent / the one with the highest ID or using a combination of fields that make the record unique. However, using the primary key is the best way to solve your issue.
另一种方法是缩小您更新的行 - 例如,仅更新最新的/具有最高ID的行或使用使记录唯一的字段组合。但是,使用主键是解决问题的最佳方法。
#3
Yes, you need the ID value of the rows you're inserting, not just the userid. I presume what you'd want to do is pass back the mysql LAST_INSERT_ID()
from your Ajax script that does the insert when you click +
, and push that ID into the information that you use later for updating the row.
是的,您需要插入行的ID值,而不仅仅是用户ID。我假设您要做的是从您执行插入的Ajax脚本中传回mysql LAST_INSERT_ID(),当您单击+时,将该ID推送到稍后用于更新行的信息中。
#4
yeah, your best bet is to have mysql return a unique id for every row created, then just make sure whenever you edit a row on your form, to send the id along with it. Now you can edit one row at a time.
是的,你最好的办法是让mysql为每一行创建一个唯一的id,然后只要确保你在表单上编辑一行就可以发送id。现在您可以一次编辑一行。