Can this be done in a single query? The table has an auto increment field which I need to know the number to fill url field in the table.
Table
id(AI) | title | url
What I am expecting is something like
INSERT INTO table (title,url) VALUES ('name','CONCATENATION OF title AND ID');
I am currently doing this using 2 queries.
1.Writing the fields except URL.
Getting the id using mysqli_insert_id()
2.Updating the above written row.
P.S : The Table has other fields as well so changing the db design isnt really possible in this case.
It can’t be done atomically. In theory, you could
SELECT MAX(id) + 1 FROM yourtable, but please, please don’t – although this is not guaranteed to give you the right result and is definitely not a safe approach.This seems like bad practice, anyway. Why not concatenate the title and ID when you fetch it? Why must it be concatenated on insert?