gpt4 book ai didi

php - 选择 LAST_INSERT_ID() *更新

转载 作者:行者123 更新时间:2023-11-29 05:43:27 25 4
gpt4 key购买 nike

我希望使用 SELECT LAST_INSERT_ID()我正在使用表单来让用户输入值。对于第一个插入,我需要为下一个插入获取最后插入的 ID...我还没有弄清楚如何获取最后选择的 ID,然后将其传递到我的第二个插入语句中

我已经更新了我的代码,但我仍然无法获取要发布到表中的 id

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (producer_id,series_id,lang_id,title_name,title_public_access) VALUES ('" . $_POST['producer_id'] . "','" . $_POST['series_id'] . "','" . $_POST['lang_id'] . "','" . $_POST['title_name'] . "','" . $_POST['title_public_access'] . "')";

$last_id = mysql_insert_id();

$query = "INSERT into `".$db_table2."` (seg_id, file_video_UNC,file_video_URL) VALUES ('" . '$last_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";

mysql_query($query);
mysql_close($link);

最佳答案

有一个函数,叫做 mysql_insert_id() .

... first query here ...
$last_id = mysql_insert_id();
$sql = "INSERT INTO $db_table SET
file_video = " . $_POST['file_video_UNC'].",
file_video_URL = " . $_POST['file_video_URL'] . ",
insert_id_of_first_query = $last_id";
...

您更新后的代码不会将查询发送到数据库 - 因此没有 INSERT,所以没有 LAST_INSERT_ID

$query = "INSERT into ".$db_table." 
(producer_id,series_id,lang_id,title_name,title_public_access) VALUES
('" . $_POST['producer_id'] . "','"
. $_POST['series_id'] . "','"
. $_POST['lang_id'] . "','" . $_POST['title_name'] . "','"
. $_POST['title_public_access'] . "')";

mysql_query($query); /* YOU FORGOT THIS PART */
$last_id = mysql_insert_id();

关于php - 选择 LAST_INSERT_ID() *更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4675554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com