[英]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.