Log in

View Full Version : Resolved Inserting records



tuga
11-20-2008, 08:37 AM
I'll try to explain what i'm trying to achieve...

I have 2 mysql tables one for items and another for images of the items.
A form where the user will be adding/editing some items.

Problem: When he goes to the form the record is not already created. The idea was to make an insert after. But the user can upload the images before saving the record or refreshing the page (uploading with iframe).
How can i establish a relationship with the images uploaded and the current record?

I thought maybe i could make an insert before go to the edit form, and then fetch the last record and do an update, but if another user is doing the same? maybe the record they fetch is the same, so i guess this is not a solution.

Any ideas how to achieve this?

Thanks

jc_gmk
11-20-2008, 09:30 AM
You could insert an ID number which is auto-incremental. that way it can't be assigned to more than one user.

tuga
11-20-2008, 09:35 AM
You could insert an ID number which is auto-incremental. that way it can't be assigned to more than one user.

Thanks but that was exactly what i'm doing.
When he submits the form it is an incremental number as ID but the problem is that the user will be uploading images before he submits the form, that i have to associate with that ID, and i don't know which is before he submits the form.


Thanks

jc_gmk
11-20-2008, 10:22 AM
Maybe you could create and insert the id before the form is even loaded?
Then send that id to the form to be used for image insertion.

you could make the code unique based on time.
e.g.


<?php
$id = rand() . time();
?>

tuga
11-20-2008, 10:36 AM
I guess i found a solution
Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.

http://php.net/manual/en/function.mysql-insert-id.php

I just make an insert and then use that value.