The autoincrement value is generated by the database itself, when the insertion is done ; which means you cannot get it before doing the actual insert query.
The solution you proposed is not the one that's often used -- which would be :
- insert some half-empty data
- get the autoincrement value that's been generated
- do your calculations, using that autoincrement value
- update the row to put the new / full data in place -- using the autoincrement generated earlier in the
where
clause of the update
query, to identify which row is being updated.
Of course, as a security precaution, all these operations have to be made in a transaction (to ensure a "all or nothing" behavior)
As pseudo-code :
begin transaction
insert into your table (half empty values);
$id = get last autoincrement id
do calculations
update set data = full data where id = $id
commit transaction
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…