I want to create id for the new post, such as 00001,00002, however,
at the same time I need to retrieve the post_id to name the picture URL xxx/00001_1.jpg
. So should I implement it with mysql(auto_increment)
or php/java API?

- 33
- 6

- 23
- 8
-
I think that ids should have no meaning beyond the scope of the database, so just have an autoincrementing id, and a unique image filepath/name. Trust the database to tell you which image belongs to which post. – Strawberry Jan 28 '19 at 08:17
3 Answers
For the schema yes, just use auto_increment.
I'm unsure about in java but there are functions in php to return the of the last item inserted: How do I get the last inserted ID of a MySQL table in PHP?

- 1,781
- 1
- 12
- 11
You can't use auto-increment because auto-increment always starts with 1. And you are using 00001. So, you have to add id manually.
First of all get last inserted id. and when you will insert new data you have to do +1 with last_inserted_id
- like this:
$last_inserted_id=SELECT LAST_INSERT_ID();
at insert time use last_inserted_id and +1.
You can run two queries at a time 1) insert the post to mysql database than get the id of last modified row. 2) update that particular row with the help of the Id that you fetched and execute update table command at that particular row Id with new name of 001.jpg Comment if you need a real working code
And auto_increment will give you numbers from 0 too so on. If you need numbers like that use php base_convert()
I am continuing with the code example I Said.
First Query be like
INSERT into table (name, post) VALUES ('$NAME', '$POST_STRING') ;
$lastID = mysqli_insert_id($con);
**Second Update query for inserting image will be like **
UPDATE table SET img = '$lastID' WHERE Id = '$lastID'

- 182
- 10
-
-
I would like to do that, however, my reputation is less than 15 points and I could not vote now.. – xiaolun Jan 28 '19 at 15:12