选择 LAST_INSERT_ID() *更新
我正在寻找使用 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);
I'm looking to use SELECT LAST_INSERT_ID()
Am using a form to have a user input values. With the first insert I need to get the last inserted id for the next insert... I have not figured out how to get the last selected id and then pass it into my 2nd insert statement
I have updated my code though I still can not get the id to post into the table
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个函数可以实现此目的,名为 mysql_insert_id()。
您更新的代码不会将查询发送到数据库 - 因此没有
INSERT
,因此也没有LAST_INSERT_ID
There's a function for that, called mysql_insert_id().
Your updated code doesn't send the query to database - as a result no
INSERT
, so noLAST_INSERT_ID
您不能仅将查询转储到 PHP 行中的字符串中。您应该在第二个查询中使用 LAST_INSERT_ID() ,或者更好的是,使用 PHP 的 mysql_insert_id() 函数来包装这在 API 中为您提供。
You can't just dump a query into a string on its own in a line of PHP. You should have used LAST_INSERT_ID() inside your second query or, better, use PHP's mysql_insert_id() function which wraps this for you in the API.
在该行中:
我认为
VALUES ('" . '$last_id' . "',
应该只是VALUES ('" . $last_id . "',
没有单个变量周围用引号引起来。In the line:
I think
VALUES ('" . '$last_id' . "',
should just beVALUES ('" . $last_id . "',
without the single quotes around the variable.