简体   繁体   中英

Updating records in table with ID from another table

Appreciate any help in advance!

I have a DB with two tables, var table and val table.

var table

varid image vartype

val table

valid sig winner varid

The tables have a 1 to many relationship, meaning there are many val's per var.

Now with that established, I have a form that asks for the input to populate that database.

  1. The user can add multiple vals in one go - basically I have the form setup using ajax so when the user completes the fields, it posts to my vale table and then refreshes the content (not the page) using ajax/jquery so the user can enter another val/record.

  2. Once the user has entered all of their vals, the user should add the var record. When that var posts to the var table, an varid is automatically assigned and should be associated with the last X number of vals the user entered.

For example

var table

varid | image | vartype
1 | test.jpg | button

val table

valid | sig | winner | varid
1 | true | true | 1
2 | false | true | 1
3 | true | true | 1

Anyway, I need help understanding the procedure or theory behind this... In my mind, it goes something like this.

  1. My ajax should have a counter to count how many vals the user enters.
  2. Within the php file that the val form is submitted to, should enter the data and then store the id of the record just entered in an array - pass it back to the page. each time a record is added, the valid will be stored
  3. when the user is finished entering vals, it will enter the var - when the php file (separate from val file) is called, it will:
    a. insert the var
    b. grab the id of the var
    c. update the records in the val table based on array of ids with the var id

does this sound right or am I totally in space somewhere?

any assistance would be greatly appreciated.

You could create a $_SESSION var and fill it with an array of all the 'valid' using MySQLS mysql_insert_id() function.

Then when making the 'var' table insert you can simply update the 'val' table rows that match the 'valid''s stored in the $_SESSION var.

Don't forget to clear the $_SESSION var once that's finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM