我已经创建了一个图片上传网站,现在我正在尝试实施标记系统。上传图像时,从html选择标记中选择要使用的标记,然后将有关图像的信息POST到我的文件中,该文件处理上传背后的逻辑。现在因为我希望我的图像能够有多个标签,所以我使用3个这样的表设置:
Table: image
Columns: id, title, description, author, date, path
Table: tags
Columns: id, tag
Table: tagsconnection
Columns: imageid, tagid
我的MySQL查询如下:
$userSearch = "SELECT username FROM user WHERE id='$author'";
$userResult = mysqli_query($conn, $userSearch);
$authorCheck = mysqli_fetch_assoc($userResult);
$sql = "INSERT INTO image (id, name, description, author, date, path)
VALUES (NULL, '".$name."','".$description."','".$authorCheck['username']."','".$date."','".$fileNameNew."')";
$result = mysqli_query($conn, $sql);
$sql2 = "SELECT * FROM image WHERE name='$name'";
$sql2 = mysqli_query($conn, $sql2);
$imageIdCheck = mysqli_fetch_assoc($sql2);
$imageid = $imageIdCheck['id'];
$sql3 = "SELECT * FROM tags WHERE tag='$tag'";
$sql3 = mysqli_query($conn, $sql3);
$tagCheck = mysqli_fetch_assoc($sql3);
$tagid = $tagCheck['id'];
$sql4 = "INSERT INTO tagsconnection (id, imageid, tagid) VALUE (NULL, '".$imageid."', '".$tagid."')";
$result4 = mysqli_query($conn, $sql4);
这样,我的图像表成功填充了图像信息,但是,我的tagsconnection表只接收标签的ID,但是没有收到上传图像的id。显然我在上传之前无法知道图像的ID,这就是我的图像标签查询在图像信息输入查询之后的原因。在我的tagsconnection表中,imageid始终为0.