繁体   English   中英

SELECT LAST_INSERT_ID()*已更新

[英]SELECT LAST_INSERT_ID() *updated

我想使用SELECT LAST_INSERT_ID()正在使用一种具有用户输入值的表单。 对于第一个插入,我需要获取下一个插入的最后一个插入的id ...我还没有弄清楚如何获取最后一个选择的id,然后将其传递给我的第二个insert语句

我已经更新了代码,但是仍然无法获取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(),或者最好使用PHP的mysql_insert_id()函数,该函数将其包装在API中。

在该行中:

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

我认为VALUES ('" . '$last_id' . "',应该只是VALUES ('" . $last_id . "',而变量周围没有单引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM