我有这个 mysql 查询:
insert into table_products values (NULL, '$product_name', '$product_description', '$image_path', '$brand_id', 1)
由于 product_id 是一个 auto_increment 主键,传递 NULL 将为其分配下一个可用整数。
它是我想分配给带有一些前字符串和后字符串的图像路径的同一个整数。
例如,如果 product_id 分配为 900,我想将 image_path 分配给 'somestring/' + 900 + '.png' 即 something/900.png
如何在插入到表中时为图像路径列获取这个 900。
最佳答案
您可能需要执行单独的查询并使用刚刚插入的 ID 更新该列,例如:
做你的插入:
insert into table_products values (NULL, '$product_name', '$product_description', NULL, '$brand_id', 1)
然后使用 mysqli_insert_id 或 PDO::lastInsertId 获取 id 并设置路径字符串 eg:
$id = mysqli_insert_id($connection);
$path = 'somestring/' . $id .'.png';
然后用路径更新上一行:
UPDATE table_products SET path = '$path' WHERE product_id = $id;
关于mysql - 如何访问其他列中的mysql分配的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815319/