0

i am creating an app that upload files in the database. for example i upload 3 files at the same time and i want their IDs to be the same. then i will upload again another batch of files and their IDs should also be the same but the id is now incremented. \

I am using two IDs DM_ID and id. DM_ID is auto incremented while id is not. I want to get the last value of id. DM_ID and id is in one table.

i tried this code but it is not working

$result = mysqli_query($con,"SELECT id from __scannedfiles order by id desc limit 1 ")  OR die(mysqli_error($con));
                $row = mysqli_fetch_array($result);
                $file_id = $row[0];


mysqli_query($con,"INSERT INTO __scannedfiles VALUES ('','$file_id'+1,'$file_title', '$file_name','$file_pic','$file_deptname','$file_doctype','$file_date','$file_desc')");
6
  • what is the error message? Commented May 9, 2013 at 6:57
  • there is no error message, the id is still not the same for a batch of files. Commented May 9, 2013 at 6:58
  • have you set auto increment for id? Commented May 9, 2013 at 7:02
  • Is ID an integer field? Otherwise order won't be as you expect Commented May 9, 2013 at 7:05
  • @Fabio it is an integer field. Commented May 9, 2013 at 7:07

2 Answers 2

1

You need to have 2 tables.

One for whatever "batches" One for files

Both with autoincremented identifier. Before inserting into files table, insert into batch table, get an id, and then use it for inserts into files table

This way you will have proper identification.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i do that? i am new to php and mysql.
how can i set the ids to be the same?
insert into batch table, get an id, and then use it for inserts into files table
0

At first sight I might think you're inserting a new row with an empty '' id, which is not permitted. Try specifically stating which attributes you're assigning the values to in the INSERT statement.

mysqli_query($con,"INSERT INTO __scannedfiles(DM_ID, DM_NANE, DM_PIC, DM_DEPTNAME, DM_DOCTYPE, DM_DATE, DM_DESC) VALUES ('$file_id'+1,'$file_title', '$file_name','$file_pic','$file_deptname','$file_doctype','$file_date','$file_desc')");

1 Comment

the empty '' if for DM_ID which is auto incremented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.