getting id of last query when multiple queries are made - php

In php have a form with a textarea and a file upload. In mysql I have table 1 with rows for each uploaded file having data like name, size, etc... and I have table 2 with the data entered in the textarea and a column with the id of the file uploaded.
I'm using mysql_insert_id to get the id of the last inserted row in table 1 (the id of file uploaded). Then I insert that id in table 2.
Assuming that multiple uploads are made at the same by different users will mysql_insert_id return the proper id each time? What happens if 2 users upload at the exact time.. Or there is a timeout.. I guess I'm asking if there's a better way to insert a newly created id to 2 tables at the same time?

It returns the id of inserted row for the session from which the insert has been made. So yes, you're doing it just fine.

mysql can't give twice the same value if of course you have the auto-increment on the primary key of the table

Per the php docs (mysqli::$insert_id) last_insert_id function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. Thats for the specific resource you are using; other resources will not affect each other

Related

Is it possible to get the ID of the row being inserted *in the statement*?

I want to have the image I'm uploading have the ID of the row it belongs to as the name (e.g. 42.jpg) to ensure uniqueness.
Is it possible to get this in the middle of the query? Or do I have to get the ID after then update the row?
You could first insert an empty row (with a null image and filename.) You can retrieve the last inserted ID with LAST_INSERT_ID().
After that, you can update the row with the image and the file name based on the ID.
If id is AUTO_INCREMENT column in the table you're inserting... no, the automatically assigned value is not available during statement execution. And the value isn't available when a BEFORE INSERT trigger is fired.
The value assigned to the row is only available after the INSERT statement completes.
Immediately following a successful insert, SELECT LAST_INSERT_ID() (equivalent library function) can retrieve the value assigned.
As another option, you could consider emulating an Oracle sequence object, using a separate table with an AUTO_INCREMENT column, and LAST_INSERT_ID. If you get that as a unique id before you do the INSERT, you could supply the value in the INSERT.

PHP and MySQL to populate table from unknown ID

I have three tables: unitpost, file, and postfile with postfile being the table that links files to a particular post.
I have created a script that uploads the files and posts however I need to populate the postfile table with both the unitpostID and fileID, however once the file is uploaded, the ID is chosen at random. How can I then populate the postfile table with the fileID that was just uploaded?
Hope this made sense!!
Any help appreciated.
Use LAST_INSERT_ID() to obtain id of last insert operation.
Use AUTOINCREMENT for the primary keys, thus the IDs won't be random, and you will be able to use them either from MySQL or from PHP.
Afterwards use mysqli_insert_id() (or PDO::lastInsertId if you use PDO) from php to get the last insert id, or, as #Nelson stated, the MySQL function directly.

MYSQL - Find the ID of the record just added

OK, so here is my scenario - you may disagree with what I'm attempting to do but I have my reasons.
The user is able to upload various information to the database, I also want them to be able to upload a picture at the same time. Now for various reasons I want to store the image with the file name the ID number of the record I'm adding (I have an ID column as the primary which auto-increments). Obviously I would have to store the file extension in the database too.
Now is there a way I can add the record and then know what ID was set so that I can save the image? I don't want to query and select the highest ID as that could go wrong if two people were to submit the form at the same time.
Any ideas?
LAST_INSERT_ID() will give you the ID that was auto-generated:
... returns a BIGINT (64-bit) value representing the first
automatically generated value that was set for an AUTO_INCREMENT
column by the most recently executed INSERT statement to affect such
a column. For example, after inserting a row that generates an
AUTO_INCREMENT value, you can get the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195
After running an insert in PHP, you can get the AUTO_INCREMENT ID from $mysqli->insert_id (where $mysqli is a MySQLi connection object), $pdo->lastInsertId (where $pdo is a PDO connection object), or mysql_insert_id() (if you're still using MySQL, which you shouldn't be) without having to run another query.

Auto Increment two fields in SQL?

I was wondering if there is a way to Auto Increment two columns in the same table.
I have database with user_id so every registred user would have his unique nr.
But I want to add a file_id so every file the user upload gets a unique number.
Any tips ?
Thanks
To ensure your database is normalized correctly, you should store file uploads in a separate table.
This means one user has the potential to have multiple files and allows the table to correctly increment the identity field of the file records.
You can read about database normalization on Wikipedia:
http://en.wikipedia.org/wiki/Database_normalization
No, only one identity column per table is allowed.
if you are using SQl Server follow this:
If your second field is static (you dont have to edit and it follows a sinmple logic to generate its value)
what you can do is create the second field as a calculated one, for example:
create table autoINC(
field1 int identity(1,1),
field2 as field1+1,
real_field varchar(50))
insert into autoINC values('test')
select * from autoINC
1 2 test
if you need to update your second field, then you need to use a trigger
I think you could use a trigger or similar solution depending on the database.
In MSSQL a trigger on update should work.
But this will not ensure unique file names unless the filename also includes the userid.
Is there a reason you need fileid to be sequential for each user?
If not, add a userid field to the file table and have a separate fileid identity column in that.

how do i handle concurrent inserts in mysql table and fetch the correct insert id

I am using adodb for PHP library.
For fetching the id at which the record is inserted I use this function "$db->Insert_ID()"
I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ?
The reason I am asking this is because I use this last insert id for further processing of other records and making subsequent entries in the related tables.
Is this approach safe enough or am I missing something.
Please help me formulate a proper working plan for this so that I can use the last insert id for further inserts into the other table safely without having to mess up with the existing data.
Thanks
Yes, it's safe for concurent use. That's because LAST_INSERT_ID() is per-connection, as explained here:
The ID that was generated is
maintained in the server on a
per-connection basis. This means that
the value returned by the function to
a given client is the first
AUTO_INCREMENT value generated for
most recent statement affecting an
AUTO_INCREMENT column by that client.
This value cannot be affected by other
clients, even if they generate
AUTO_INCREMENT values of their own.
This behavior ensures that each client
can retrieve its own ID without
concern for the activity of other
clients, and without the need for
locks or transactions.
The $db->Insert_ID() will return you last insert id only so if you are inserting many records and want to get id of each last inserted row, then this will work successfully.
I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ?
It will return only the most recently inserted id.
In order to get ids for multiple inserts, you will have to call INSERT_ID() after each statement is executed. IE:
INSERT INTO TABLE ....
INSERT_ID()
INSERT INTO TABLE ....
INSERT_ID()
...to get the id value for each insert. If you ran:
INSERT INTO TABLE ....
INSERT INTO TABLE ....
INSERT_ID()
...will only return the id for the last insert statement.
Is this approach safe enough or am I missing something.
It's safer than using SELECT MAX(id) FROM TABLE, which risks returning a value inserted by someone else among other things relating to isolation levels.

Categories