0

我正在创建一个在数据库中上传文件的应用程序。例如,我同时上传 3 个文件,我希望它们的 ID 相同。然后我将再次上传另一批文件,它们的 ID 也应该相同,但现在增加了 ID。\

我正在使用两个 ID DM_ID 和 id。DM_ID 是自动递增的,而 id 不是。我想得到id的最后一个值。DM_ID 和 id 在一个表中。

我试过这段代码,但它不工作

$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')");
4

2 回答 2

1

您需要有 2 张桌子。

一个用于任何“批次” 一个用于文件

两者都具有自动递增的标识符。在插入文件表之前,插入批处理表,获取一个id,然后将其用于插入文件表

这样,您将获得正确的身份证明。

于 2013-05-09T07:07:05.170 回答
0

乍一看,我可能会认为您正在插入一个''id 为空的新行,这是不允许的。INSERT尝试在语句中明确说明您将值分配给哪些属性。

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')");
于 2013-05-09T06:58:08.337 回答