我正在尝试使用以下语句将照片信息插入照片表:
$stmt = $mysqli->prepare("INSERT INTO Photographs(Genre, Name, Photographer, Camera, location) VALUES(?,?,?,?,?)");
$stmt->bind_param('isiis', $genre, $name, $photographer, $camera, $location);
$stmt->execute();
成功插入后,我想获取照片表的自动递增主键值并插入联结表(对于我的多对多关系):
$stmtPhotoGenre = $mysqli->prepare("INSERT INTO PhotoGenre(idPhotograph, Genre) VALUES(?,?)");
$stmtPhotoGenre->bind_param('ii', $idPhotograph);
有没有办法使用 MySQL 或 PHP 获取最后插入的 ID?我想过使用时间戳,但总是有可能两个人可以同时插入,并且可能会发生某种交叉映射。有什么解决方案或建议吗?
最佳答案
mysqli_insert_id — 返回上次查询中使用的自动生成的 id
关于PHP MySQL INSERT 值返回插入的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501612/