1

I do not actually know much about PHP and jQuery. I am a self-taught programmer, lately I've focused on web programming with PHP for the server-side and JavaScript for client-side.

AJAX is a relatively new concept for me, I'm just doing the first tests.

I'll explain my problem, surely what I want, it should be trivial to many.

I have a rather long form, it have checkboxes, text inputs, textareas and selects, further, I want to add the bootstrap file-input plugin to my form.

The problem is that I want to send all the data together, and I can’t send the photos before and after send the data, or send the data and then send the photos.

The reason for this is that my database is relational, and I need to associate the data and pictures to the same ID, which must be self-generated, is an autoincrement field.

Imagine I have a table called data and another table called data_ref_pictures And in my form there are only these fields name, gender and age:

data(id, name, gender, age)
data_ref_pictures(id_data, picture_path)

I am very concerned because I think my system is mono-user/single-user :-(

Imagine that there are 100 users in different places, and they all at once are using the form, suppose now that the form is already filled and that the photos are selected too (without sending anything yet), the form is ready to send, and all 100 users simultaneously press the submit button.

How can I know the id number of the data, in order to avoid that the system makes any mistakes and mix the data and pictures of users.

The problem is that I don’t know to associate all data and pictures sent by the user with a unique autoincrement id.

Thanks for reading!

candlejack
  • 1,189
  • 2
  • 22
  • 51
  • The solution may depend on the organization of your pages, which isn't clear. Is all the data sent with the same form? If so, it should be trivial to insert everything in an order that allows you using last_insert_id. If not, you may need to use session handling to keep track of the submitted data. If the images may come before the data in the main table, then you need some way to identify the user they come from until the data for the main table comes. Not so simple if that's the case. – Pedro Gimeno Feb 26 '15 at 02:35
  • I still do not set the plugin file-input, I need to do with [AJAX](http://plugins.krajee.com/file-input/demo#ajax-sync), is in the same form, yes, but not yet know if sent together. – candlejack Feb 26 '15 at 02:40

1 Answers1

2

You can use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

The result of the function is based on the last insert done by the active connection so you shouldn't have conflicts inserting your data.

Jordi Llull
  • 810
  • 6
  • 17