views:

2212

answers:

4

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.

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.

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.

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?

Thanks for any suggestions you can give, this one is clouding my mind currently...

+1  A: 

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.

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.

Sohnee
+1  A: 

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.

chaos
what would be the best way of getting the LAST_INSERT_ID() sent back to the main form? Would a $_SESSION be the solution?
Choog
My thought was that you would pass it back as JSON or XML from the script.
chaos
+1  A: 

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.

Renato Aquino
+1  A: 

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.

justlost
Do you have a suggestion of how to actually get the returned unique id for every row created? I see where you're coming from but unsure how to put it into practice.
Choog
you can:- add "; select last_insert_id();" at the end of your insert query to get the last inserted id.- you can get the id by calling mysql_insert_id ();- you can get the id by calling mysqli->insert_id();check the php references: [http://www.php.net/manual/en/function.mysql-insert-id.php] and and [http://www.php.net/manual/en/mysqli.insert-id.php]then send the id back to through json to jquery, check out [http://docs.jquery.com/Ajax/jQuery.ajax] for examplesthen on every update you just pass the id of the row you want to update
justlost